简体   繁体   中英

Using a regex for email address verification

I need help with a regex and I don't know how to go about this.

How can I prevent the user from submitting until they enter a proper email?

The email format must be like this emailaddress@bri.golia.com

the email address that should be accepted can only have the domain @bri.golia.com, anything else should not let the user submit the form, how can I go about this?

            <form method="post" action="site.com/mail.php" name="SampleForm" enctype="multipart/form-data">


                        <div class="info">
                            <h2 class="forms_heading" name="Contact">Contact Information</h2>
                                <br />
                                <div class="required">
                                    <label for="name">Name:</label>
                                    <input type="text" for="name" id="name" name="RequesterName" required="required"  />

                                </div>
                                <div class="required">
                                    <label for="email">E-mail:</label>
                                    <input type="text" for="email" id="email" name="RequesterEmail" required="required"   />
                                </div>

                                <div class="required">
                                    <label for="phone">Phone:</label>
                                    <input type="text" for="phone" id="phone" name="RequesterPhone" required="required"   />
                                </div>

                                <br />
                                <br />
                        </div><!-- end info -->





                            <br />
                            <input type="submit" value="Submit" class="submit_button"  />

Assuming that you're using the jQuery Validation Plugin , you should be able to add a custom regex validation method.

I haven't tested this, but it should be close:

$.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var check = false;
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Please check your input."
);

With the above method, now you can pass in the validation regex:

$("#input1").rules("add", { regex: "^[a-zA-Z]+@yourdomain\.com$" });

Obviously, you can tweak the regex as needed until it satisfies your requirements. For example purposes, I've included a basic regex to validate the domain name.

Regex string

^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$

I would either use an ASP validator of RegEx type, or do a Regex compare on the button click method.

A few other RegEx strings can be found here:

Here is some sample ASP for the Text field and RegEx validator:

    <asp:TextBox ID="txtEmail" runat="server" />
    <asp:RegularExpressionValidator ID="valEmail" ControlToValidate="txtEmail" 
            runat="server"
            ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"
            ErrorMessage="You need a valid e-mail address" /> 

Also, here is a good search for more on validators in ASP

  • Google: asp regex validator

Add the pattern attribute with the regex:

<input type="text" for="email" id="email" name="RequesterEmail" required="required" pattern="\b[A-Za-z0-9._%+-]+@bri\.golia\.com\b"  />

Also see my jsfiddle .

=== UPDATE ===

To change the error text overwrite the invalid method:

document.getElementById('email').oninvalid = function(e) {
    e.target.setCustomValidity('');
    if (!e.target.validity.valid) {
        e.target.setCustomValidity("My custom text.");
    }
};

Also see my updated jsfiddle .

i dont think you need to use a regexp for this, a simple substring with indexOf is anuf to extract the first and last part of the email to then check if its @bri.golia.com

var str_email = "testblah@bri.golia.com";

var str_at_domain = str_email.substring(str_email.indexOf('@'));

var userEmail = str_email.substring(0, str_email.indexOf('@'));

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