Javascript Email syntax validation for multiple forms

Hello,

I need the Javascript (and HTML for submit) for a function that can take the name of a form and the name of the email text field and validate it.

Thus, the one function could work with multiple forms on a single page.

If the syntax is wrong, a pop-up alert is generated.

If the syntax is right, the form is automatically submitted.

Thanks in advance.

Steve
skbohlerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Check the HTML5 type : https://developer.mozilla.org/fr/docs/Web/HTML/Element/Input/email
please note the required attribute :
<input id="adresseEmail" type="email" required />

Open in new window

It display an alert and prevent the form to submit

Else, without HTML5 type "email" :
This is the code from jQuery validation plugin :
		// https://jqueryvalidation.org/email-method/
		email: function( value, element ) {

			// From https://html.spec.whatwg.org/multipage/forms.html#valid-e-mail-address
			// Retrieved 2014-01-14
			// If you have a problem with this implementation, report a bug against the above spec
			// Or use custom methods to implement your own email validation
			return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
		},

Open in new window


From it, wez can write soemthing like :

var checkEmail = function(formName, fieldName) { // you should use ID attribute instead Name attribute
         var value = '';
         var form = document.getElementsByName(formName);
         if(form.length) {
            var field = form[0].getElementsByName(fieldName)[0];
            if(field.length) {
                value = field[0].value;
            }
            else {
                alert("field" + fieldName + " not found");
                return false;
            }
         }
         else {
            alert("form " + formName + " not found");
            return false;
         }
         if(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value )) {
               return true;
         }
         else {
               alert("email : " + fieldName + " not good");
               return false;
         }
};

Open in new window


<form onsubmit="return checkEmail(this, this.email);" action="foo.xyz">
        <input type="text" name="email" />

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skbohlerAuthor Commented:
The HTML5 approach works well. But, I want to test the Jquery version too.

I inserted the code.

My HTML looks like:

<form method=post name=frmSF action = "/mailpass.asp" class="form-main" onSubmit="return checkEmail(this,this.UserEmail2);">
        <div class="input-row">
                            <label class="label-block" for="email_address">
                                                Enter Your Email Address: </label>
<input type=text name="UserEmail2" class="input-text" style="width: 300px;">
       <p></p>
<INPUT class="button-blue-dark button-round button-shadow" type="submit" name="Action" value="Retrieve Your Username">
      </INPUT>
       </p>
       </div>
</form>            

When I click the submit button, I get the pop-up telling me:

form [object HTMLFormElement] not found

Any suggestions?
0
leakim971PluritechnicianCommented:
check this out :
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        var checkEmail = function(formName, fieldName) { // you should use ID attribute instead Name attribute
            var value = '';
            var form = document.getElementsByName(formName);
            if(form.length) {
                var field = form[0][fieldName];
                if(field) {
                    value = field.value;
                }
                else {
                    alert("field" + fieldName + " not found");
                    return false;
                }
            }
            else {
                alert("form " + formName + " not found");
                return false;
            }
            if(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value )) {
                return true;
            }
            else {
                alert("email : " + fieldName + " not good");
                return false;
            }
        };

    </script>
</head>
<body>
<form method=post name=frmSF action = "/mailpass.asp" class="form-main" onSubmit="return checkEmail(this.name,this.UserEmail2.name);">
    <div class="input-row">
        <label class="label-block" for="email_address">
            Enter Your Email Address: </label>
        <input type=text name="UserEmail2" class="input-text" style="width: 300px;">
        <p></p>
        <INPUT class="button-blue-dark button-round button-shadow" type="submit" name="Action" placeholder="Retrieve Your Username" />

        </p>
    </div>
</form>
</body>
</html>

Open in new window


or :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        var checkEmail = function(fieldID) { // you should use ID attribute instead Name attribute
            var value = document.getElementById(fieldID).value;
            if(/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value )) {
                return true;
            }
            else {
                alert("email not good");
                return false;
            }
        };

    </script>
</head>
<body>
<form method=post name=frmSF action = "/mailpass.asp" class="form-main" onSubmit="return checkEmail('UserEmail2');">
    <div class="input-row">
        <label class="label-block" for="email_address">
            Enter Your Email Address: </label>
        <input type=text name="UserEmail2" id="UserEmail2" class="input-text" style="width: 300px;">
        <p></p>
        <INPUT class="button-blue-dark button-round button-shadow" type="submit" name="Action" placeholder="Retrieve Your Username" />
    </div>
</form>
</body>
</html>

Open in new window

0
skbohlerAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.