Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

custom validation error messages


Hello Experts,

Look at code below. i use a custom validation "alpha" for the "First Name" field validation.
when the field is left empty i prints the custom message "Please enter your First Name" but also when i enter numbers. It ignores the "Only Characters Allowed"?

jQuery.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z]+$/);
},"Only Characters Allowed.");


rules: {
FirstName: "required alpha",
},

messages: {
FirstName: "Please enter your First Name",
},

Open in new window

0
Refael
Asked:
Refael
  • 2
1 Solution
 
Kyle HamiltonData ScientistCommented:

Try:

rules: {
FirstName: {
                   required: true,
                   alpha: true
}
0
 
RefaelAuthor Commented:


thanks kozaiwaniec
but it does not help, still prints only the "Please enter your First Name".
0
 
sonawanekiranCommented:
Look at this complete example.
Test page http://jsfiddle.net/ur7aN/2/


<html>
<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js" type="text/javascript"></script>

<script>
$(document).ready(function(){
     
        $.validator.addMethod("alpha", 
                              function(value, element) {
                                  return this.optional(element) || /^[a-zA-Z\s]+$/.test(value);
                              }, 
                              "Alpha Characters Only."
       );
       
       $("#TestForm").validate({
           rules: {
               FirstName: {
                   required: true,
                   alpha: true
               }
           },
           messages: {
               FirstName: {
                   required: "Please enter your First Name"
               }
           }    
       });
  })

</script>
</head>

<body>
<form id="TestForm" name="TestForm">
<input type="text" name="FirstName" id="FirstName" />
<input type="submit">
</form>


</body>

</html>

Open in new window

0
 
RefaelAuthor Commented:
sonawanekiran thank you, and thank you for the online example :-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now