Actually I am new to jquery as well as web designing
Now I need some piece of code which will invoke after blur event on a text box.
I need jquery code for validating a name field in the html form.
Actually I am new to jquery as well as web designing
Now I need some piece of code which will invoke after blur event on a text box.
I need jquery code for validating a name field in the html form.
Try this - http://jsfiddle.net/gZNkt/
$("#fname").on("blur", function() {
if ( $(this).val().match('^[a-zA-Z]{3,16}$') ) {
alert( "Valid name" );
} else {
alert("That's not a name");
}
});
It will check your name to be 3-16 characters long and contain only letters, so sam will validate, but 3sam - won't
Make sure you add a latest version of jQuery.... Here is a sample for validating for not null.
$(document).ready(function() {
$('input#fname').on('blur', function() {
if( $(this).val() == '' || $(this).val() == "null") {
// Your code to handle error
} else {
return true;
}
})
});
32#78@*! or 3sam as a name $("#reg").bind("keypress", function (event) {
if (event.charCode!=0) {
var regex = new RegExp("^[a-zA-Z]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
}
});
you disable press key and allow only charecters ....
I think you want to validate your text box after focus out or blur in jQuery.
You can use below code for the same purpose :-
$(document).ready(function()
{
$("#fnam").blur(function() {
if($(this).val() == null || $(this).val() == undefined || $(this).val() == "")
{
alert("field is empty");
}
});
// if you want this validation on focus lost
$("#fnam").focusout(function() {
if($(this).val() == null || $(this).val() == undefined || $(this).val() == "")
{
alert("field is empty");
}
});
});
Why dont you try with the jQuery Validate plugin
<input id="cname" name="name" size="25" class="required" minlength="2" /> And then in you javascript you just do:<br/> $('#yourForm').validate();