110
rated 0 times
[
111]
[
1]
/ answers: 1 / hits: 27250
/ 11 Years ago, tue, april 30, 2013, 12:00:00
I have managed to adapt a jquery form validation example to work with my site, but I don't know how to make sure it's just numbers entered in the phone number field and just email addresses entered in the email field.
Please can anyone help?
Also wondering if there is anything that could be improved?
<script language=javascript type=text/javascript src=http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js></script>
<script>
$(function() {
$( #quickform ).validate({
rules: {
name: {
required: true
},
phone: {
required: true,
phonevalidation: true
},
email: {
required: true,
emailvalidation: true
},
enquiry: {
required: true
}
},
messages: {
name: {
required: Please enter your name.
},
phone: {
required: Please enter your phone number.
},
email: {
required: Please enter your email address.
},
enquiry: {
required: Please enter your enquiry.
}
},
});
$.validator.addMethod(phonevalidation,
function(value, element) {
return /^[A-Za-zd=#$%@_ -]+$/.test(value);
},
Please enter a valid phone number.
);
$.validator.addMethod(emailvalidation,
function(value, element) {
return /^[A-Za-zd=#$%@_ -]+$/.test(value);
},
Please enter a valid email address.
);
});
</script>
EDIT:
There are already validation methods in the example but it is to not accept special characters. I just need to understand how to change the /^[A-Za-zd=#$%@_ -]+$/ to email and number validation.
More From » jquery