Iam not understanding to do validation for the 'FullName' field..
Below are the validations required for the 'FullName' field:
- only letters a to z (lower case), "-" (dash or hyphen) and " " (space) are allowed,
- the "-" (dash) AND " " (space) letters MUST be entered,
- the "-" or the " " letter must not be either the first or the last letter entered,
- "-" must not be the immediate neighbour or adjacent (before or after) to a " ",
- "-" or " " must not be the immediate neighbour (adjacent) to itself.
I knew I can do in this way:
$('#fullName').blur(function(){
var input = $('#fullName').val();
if( !/[^a-z0-9 -]/.test(input) &&
/ /.test(input) && /-/.test(input) &&
!/^[ |-]|[ |-]$/.test(input) &&
!/ -|- |--| /.test(input))
{
$('.error').remove();
}
else{
$('#fullName')
.after('<span class="error">Your Name should be entered like: "blahblah" </span>');
}
});
BUT I am not understanding how to insert above regex code into here:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript" src="http://jquery-joshbush.googlecode.com/files/jquery.maskedinput-1.2.1.pack.js"></script>
<script>
$(document).ready(function(){
$("#fullname").focus();
$("#fullname").addMethod("alphanumeric", function(value, element) {
return ! $("#fullname").methods.required(value, element) || /^[a-zA-Z0-9_]+$/i.test(value);
} , "Letters, numbers or underscores only please");
$("#ourform").validate({
onfocusout: function(element) { $(element).valid(); } ,
rules: {
fullname : {
required: true,
maxlength: 14,
alphanumeric : false
},
email: {
required: true,
email: true
}
},
messages: {
fullname : {
required: "Please specify your Full Name",
maxlength: "Please enter only upto 14 characters",
alphanumeric : "do not enter alphanumeric"
},
email: {
required: "We need your email address to contact you",
email: "Your email address must be in the format of name@domain.com"
}
}
});
});
</script>
<style>
.error {color: red;}
</style>
</head>
<body>
<form id="ourform" method="get" action="">
<fieldset>
<p>
<label for="fullname">Full Name</label>
<em>*</em><input id="fullname" name="fullname" size="25" class="required" maxlength="14" />
</p>
<p>
<label for="email">Email</label>
<em>*</em><input id="email" name="email" size="25" class="required email" />
</p>
</fieldset>
</form>
</body>
</html>
EDITED: - FullName (both first and family name - use ONE field for both),