mrrsb
mrrsb

Reputation: 681

jquery formvalidation engine add method

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).

Upvotes: 0

Views: 946

Answers (2)

doptrois
doptrois

Reputation: 1570

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/

Upvotes: 3

user1432124
user1432124

Reputation:

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>

Upvotes: 0

Related Questions