问题
Check out the code:
<script type="text/javascript">
function ValidateTextBox(source, args) {
var is_valid = false;
//Regex goes here
var regex = /^[a-z A-Z]+$/;
var check = regex.test($('tbName').val()); //Checks the tbName value against the regex
if (check == true) {
//If input was correct
is_valid = true;
}
else {
//If input is not correct
$("tbName").css(("background-color", "#A00000"), ("border-color", "#780000"));
}
args.IsValid = is_valid; //Returns validity state
}
</script>
Im trying to check the input of a textbox so its only character between a and z, and A and Z, but it still returns true even on input like "1245".
Why is this?
Thanks
回答1:
$('tbName')
may not be a valid selector.
Did you mean to select a class?
$(.tbName')
What about an element with an id=tbName
?
$('#tbName')
Also, why do you need to do this? This will NOT be accessible outside of the function, as it is a local variable passed to the function (via its parameters)
args.IsValid = is_valid;
You can just do a simple return:
function ValidateTextBox() {
var regex = /^[a-z A-Z]+$/;
return regex.test($('#tbName').val());
}
来源:https://stackoverflow.com/questions/7919734/javascript-to-check-validation-and-change-textbox-color