You can use regular old javascript for that:
function isEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
Answer from Fabian on Stack OverflowYou can use regular old javascript for that:
function isEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
jQuery Function to Validate Email
I really don’t like to use plugins, especially when my form only has one field that needs to be validated. I use this function and call it whenever I need to validate an email form field.
function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}
and now to use this
if( !validateEmail(emailaddress)) { /* do stuff here */ }
if ($('#mtQuiz').length > 0) { // <- unnecessary and superfluous
$('#myQuiz').validate({
// options, etc.
});
}
You're using jQuery so you don't need to check for the existence of #myQuiz with if ($('#mtQuiz').length > 0). If the #myQuiz element doesn't exist, jQuery will simply ignore it without any errors.
This is all you need to do...
$('#myQuiz').validate({ // initialize the plugin
// options, etc.
});
To match the value of another field, simply use the equalTo rule. While using the equalTo rule, there is no need to duplicate any of the other rules since equalTo will always force the value to match the primary field's value which already followed its rules.
$('#myQuiz').validate({
// options, etc.,
rules: {
'entry[first_name]': 'required',
'entry[last_name]': 'required',
'entry[email]': {
required: true,
email: true
},
'entry[confirm_email]': {
//required: true, // <- redundant, not needed with 'equalTo'
//email: true // <- redundant, not needed with 'equalTo'
equalTo: '[name="entry[email]"]' // <- any valid jQuery selector
}
},
// other options, etc.
});
DEMO: http://jsfiddle.net/JCY2E/
Have a look to equalTo method.
$('#myQuiz').validate({
...
rules: {
...
'entry[confirm_email]': {
equalTo: "entry[email]"
}
}
});
Videos
if($("#author_email").val() != $("#author_confirm_email").val())
{
alert("emails don't match, sucka");
}
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
function validation(){
var firstmail = $("#author_email").val();
var secondmail = $("#author_confirm_email").val();
if(firstmail.length != 0){
if(!isValidEmailAddress(firstmail)){
$("#error").html('please fill first mail valid');
}
}
else
$("#error").html('first mail is required');
if(secondmail.length != 0){
if(!isValidEmailAddress(secondmail)){
$("#error").html('please fill second mail valid');
}
}
else
$("#error").html('second mail required');
if(firstmail != secondmail){
$("#error").html('mail not match');
}
}
maybe it help you to solve puzzle.