0

How to create a method that check if an email is valid, if the validator find an '@' sign? (the input text is either name or email address only).

3
  • first of all always do form validation with server-side script
    – user1432124
    Jun 5, 2012 at 10:23
  • You mean if input contains @ then check for email validation otherwise check for name validation? Jun 5, 2012 at 10:24
  • @PriyankPatel yes your right..
    – mrrsb
    Jun 5, 2012 at 10:45

2 Answers 2

3
function checkMail(email){
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email)) {
        return true;
    }
    return false;
}

checkMail('[email protected]'); // return true or false if invalid.

This will check the e-mailaddress additionally if a valid domain is written.

me@something --> invalid
@something.com --> invalid
[email protected] --> invalid
[email protected] --> valid

Regular Expression Library -->

If you want to validate the mail address with jQuery Validation Engine, simply add:

data-validation-engine="validate[required,custom[email]]"

to the input field.


Since Form Validation Engine 1.6.4, there's the possibility to add your own function for validation: http://www.position-absolute.com/news/form-validation-engine-1-6-4/

You can replace the code in the example with this, then it should work:

<input value="" data-validation-engine="validate[required,funcCall[nameOrMail]]" class="text-input nameOrMail" type="text" name="req" id="req" />

JS (To your <head>):

jQuery(document).ready(function(){
    jQuery("#formID").validationEngine({
        "nameOrMail": {
            "nname":"nameOrMail"
        }
    });
});

function checkMail(email){
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email)) {
        return true;
    }
    return false;
}

function nameOrMail(){
    if($(".nameOrMail").val().indexOf('@') >= 0){
        if(!checkMail($(".nameOrMail").val())) {
            return "Please enter a valid mail address or name!";
        }
    } else {
        if($(".nameOrMail").val() === '') {
            return "Please enter a valid mail address or name!";
        }
    }
}

Live demo: http://jsfiddle.net/4dUyX/

7
  • Im using jQuery.validationEngine position-relative.net/creation/formValidator How can I add this to formValidator engine?
    – mrrsb
    Jun 5, 2012 at 10:40
  • I've added this to my answer :)
    – doptrois
    Jun 5, 2012 at 10:46
  • This documentation is pretty fine. There you'll find a lot of predefined expressions.
    – doptrois
    Jun 5, 2012 at 10:50
  • jQuery.validationEngine has this validation.. what if sir using a custom email is optional. if input contains @ then check for email validation otherwise check for name validation?
    – mrrsb
    Jun 5, 2012 at 10:50
  • My fault. I've coded a few snippets, see above. Hope this helps.
    – doptrois
    Jun 5, 2012 at 12:21
0

Use split function then you will not need regex

<script>
function check(email){
    if (email.split("@").length>0) {
        return true;
    }
   else return false;
}

check('[email protected]'); </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.