Reputation: 481
$(document).ready(function(){
$("#name").blur(function(){
var name = $("#name").val();
if ( $(this).val().match('^[a-zA-Z]{3,16}$') )
{
$("#check_name").html("Good Name").hide();
}
else {
$("#check_name").html("That's not a name.");
}
It is inserting the values if it is wrong.It is not validating data when we submit a form.need help soon.
Upvotes: 0
Views: 58
Reputation: 22490
Try with regex pattern /pattern/g
$(this).val().match(/^[a-zA-Z]{3,16}$/g)
Instead of
$(this).val().match('^[a-zA-Z]{3,16}$')
It is not validating?
Because You are matching the string not with pattern
$(document).ready(function(){
$("#name").blur(function(){
var name = $("#name").val();
if ( $(this).val().match(/^[a-zA-Z]{3,16}$/g) )
{
$("#check_name").html("Good Name").hide();
}
else {
$("#check_name").html("That's not a name.");
}
Upvotes: 0
Reputation: 4475
Use .test instead of .match to check for occurence. Also, is your regex correct, will it handle every name? Validate the regex somewhere on regex tester as well.
Https://stackoverflow.com/questions/8606117/javascript-regexp-using-val-match-method
Upvotes: 0
Reputation: 748
This will work:
$(function(){
$("#name").blur(function(e){
var name = $(e.target).val();
if ( name.match('^[a-zA-Z]{3,16}$') ) {
$("#check_name").html("Good Name").hide();
}else {
$("#check_name").html("That's not a name.").show();
}
})
});
If you also want to see "Good Name", remove the .hide() ;)
Upvotes: 0
Reputation: 1231
You need to actually do validate on the form.
After you have added jquery validation and set it up you can use
<form id="myForm">
<input type="submit" value="Send"/>
</form>
<script>
$("#myForm").submit(function(){
$("#myForm").validate();
};
</script>
Upvotes: 1