希望,你们都做得很好。
我正在尝试使用Javascript验证表单的firstname输入字段。出于某种原因,错误消息不会按顺序显示。其中一些覆盖其他,只显示一条错误消息,其余的不显示。
我想知道为什么?请问有人能给我一些帮助吗?
这是我的代码:
// Predefined validator function to check if input is empty or not
var validator = {};
validator.isEmpty = function(input) {
// Stop execution if input isn't string
if (typeof input !== 'string') return false;
if (input.length !== 0) {
for (var i = 0; i < input.length; i++) {
if (input[i] !== " ") {
return false;
}
return true;
}
}
return true;
};
validator.isEmpty(null); // returns false
// Main part to get inputs and apply validation
window.onload = function() {
var signUp = document.getElementById("signUp");
var fName = document.getElementById("fName");
var suButton = document.getElementById("subMit");
// Submit button on the function
suButton.addEventListener('click', function(event) {
isNameValid(fName);
});
signUp.addEventListener('submit', function(event) {
event.preventDefault();
});
function isNameValid(char) {
var val = char.value;
if (validator.isEmpty(val)) {
if (val.length < 2) {
// Display a message if input length is less than 2
char.setCustomValidity("We expect your input should contain at least 2 characters, darling !");
char.style.borderColor = "red";
}
if(!isNaN(val)) {
char.setCustomValidity("Please, enter only characters");
char.style.borderColor = "red";
}
} else {
char.setCustomValidity("");
char.style.borderColor = "green";
}
}
&#13;
<form id="signUp">
<input type="text" id="fName" name="firstname" placeholder="First name">
<input type="checkbox" name="result" required autofocus> Agree our conditions
<input type="submit" id='subMit' value="SUBMIT">
</form>
&#13;
答案 0 :(得分:0)
我花了一段时间,但我希望以下为你效劳。如果您需要帮助了解任何事情,请告诉我。我觉得你的代码有点复杂,所以我简化了它。
int decimal, radix, pow = 1, temp;
printf("enter your number and the 2 <= radix <= 16\n");
scanf("%d%d",&decimal, &radix);
temp = decimal;
for(; temp >= radix; temp /= radix)// finding the greatest exponent of radix in decimal
pow *= radix;
while(pow >= 1){
decimal -= temp * pow;
if(temp == 10)
printf("A");
else if(temp == 11)
printf("B");
else if(temp == 12)
printf("C");
else if(temp == 13)
printf("D");
else if(temp == 14)
printf("E");
else if(temp == 15)
printf("F");
else
printf("%d",temp);
pow /= radix;
temp = decimal / pow;
}
puts("");