/ Published in: jQuery
                    
                                        
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
<!--<div id="errorbox"><ul></ul></div>-->
$(document).ready(function(){
$("#commentForm").validate({
errorContainer: "#errorbox",
errorLabelContainer: "#errorbox ul",
wrapper: "li",
rules: {
name: "required",
email: {
required: true,
email: true,
},
comment: "required"
},
messages: {
comment: "This is a comment form. Why in the heck would you leave the comment blank?",
email: {
required: "Email address is required.",
email: "Email addresses are of the form user@host. Not yourRstupid."
},
name: {
required: "Stand up for your comments or go home."
}
}
});
});
//
$(document).ready(function(){
$("#commentForm").validate({
rules: {
name: {
required: true,
minlength: 2
},
email: {
required: "#csub:checked",
email: true,
},
comment: "required"
},
messages: {
comment: "This is a comment form. Why in the heck would you leave the comment blank?",
email: {
required: "Dude, you want the newsletter, but you didn't enter an email address?",
email: "Email addresses are of the form user@host. Not yourRstupid."
},
name: {
required: "Stand up for your comments or go home.",
minlength: jQuery.format("You need to use at least {0} characters for your name.")
}
}
});
});
//
$(document).ready(function(){
$.validator.addMethod("noanon", function(value) {
return value.toLowerCase().indexOf("anonymous") != 0;
}, 'Do not hide behind the cover of anonymity');
$("#commentForm").validate({
rules: {
name: {
required: true,
minlength: 2,
noanon: true
},
email: {
required: "#csub:checked",
email: true,
},
comment: "required"
},
messages: {
comment: "This is a comment form. Why in the heck would you leave the comment blank?",
email: {
required: "Dude, you want the newsletter, but you didn't enter an email address?",
email: "Email addresses are of the form user@host. Not yourRstupid."
},
name: {
required: "Stand up for your comments or go home.",
minlength: jQuery.format("You need to use at least {0} characters for your name.")
}
}
});
});
//
<tr>
<td>Phone</td>
<td><input name="phone" type="text" id="phone" class="required NumbersOnly"></td>
</tr>
<script>
$(document).ready(function(){
$.validator.addMethod("NumbersOnly", function(value, element) {
return this.optional(element) || /^[0-9\-\+]+$/i.test(value);
}, "Phone must contain only numbers, + and -.");
$("#regForm").validate();
});
</script>
<script>
$(document).ready(function(){
$.validator.addMethod("username", function(value, element) {
return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
}, "Username must contain only letters, numbers, or underscore.");
$("#regForm").validate();
});
</script>
Finally, i somehow got hold of the code that does the job through regular expression regex that checks for only numbers and a-z
1
2
3
4
5
6
7
8
9
10
<script>
$(document).ready(function(){
$.validator.addMethod("noSpecialChars", function(value, element) {
return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
}, "Username must contain only letters, numbers, or underscore.");
$("#myForm").validate();
});
</script>
URL: http://www.coldfusionjedi.com/index.cfm/2009/2/10/An-Introduction-to-jQuery-and-Form-Validation-2
Comments
 Subscribe to comments
                    Subscribe to comments
                
                