简体   繁体   中英

Validating more that one email field

Im using Joren Rapini's validation code to for my online form. Joren's Website

It allows you to check to see if an email address has been correctly entered, but I have 2 email addresses that I want to validate in the form.

Any idea how I would do this?

I've tried adding in email = $("#youremail"); as well as the one that's currently in the code which is email = $("#receiveremail"); but it only works for one.

$(document).ready(function(){
    // Place ID's of all required fields here.
    required = ["youremail", "name", "receiveremail", "message"];

    // If using an ID other than #email or #error then replace it here
    email = $("#receiveremail");
    errornotice = $("#error");

    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field";
    emailerror = "Not a vaild email";
});

He doesn't do a great job of explaining this, but it's fairly straightforward. You have to validate two separate email fields:

if (!/^S+@S+.S+$/.test(email.val())) {
    email.addClass("needsfilled");
    email.val(emailerror);
}
var email2 = $("#youremail");
if (!/^S+@S+.S+$/.test(email2.val()) {

Of course it would be better to loop over the emails in a similar setup to how required is done.

Use var too.

You need to run the validation code for your other field

in document.ready

otheremail = $("#otheremail");

in form submit

if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(otheremail .val())) {
            otheremail .addClass("needsfilled");
            otheremail .val(emailerror);
        }

No offense to Joren Rapini but this code is hacky and shouldn't be used for other than the smallest purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM