onChange validation for submit
For a field I have validation for onChange() event. When I change the field and click on submit button. Does the onChange() events checks the validation.
In my case it is not checking.
Any help or suggestions please
<textarea id='abc' name='abc' onChange="checkvalidation('abc','true')>ddd</textarea>
function checkvalidation(field, numericOrNot){
var Value = '#' + field;
var fieldval = $(field).v开发者_C百科al();
if(numericOrNot) {
if(isNaN(fieldval)) {
alert("not a number");
return false;
}
}
return true;
}
The onChange event should fire when you've changed the value of a form field and remove focus from that field. It should fire before you click the submit button.
However, you're missing a closing quote after the )
in your html.
<textarea id='abc' name='abc' onChange="checkvalidation('abc','true')">ddd</textarea>
//-------------------------------------------------------------------^
Here's how to change your code so that it uses a data-
attribute to determine the data type.
Html
<textarea id='abc' name='abc' data-type="number">ddd</textarea>
jQuery Code
$("#abc").change(function() {
var $this = $(this);
if($this.data("type") == "number") {
if(isNaN($this.val())) {
alert("not a number");
}
}
});
Example on how to run this code when the form is submitted
$("form").submit(function(e) {
var $this = $("#abc");
if($this.data("type") == "number") {
if(isNaN($this.val())) {
alert("not a number");
e.preventDefault();
}
}
});
精彩评论