0

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"  />
bakoyaro
  • 2,550
  • 3
  • 36
  • 63
Bulvak
  • 1,794
  • 8
  • 31
  • 63
  • 1
    If the email address has to have that domain, why even ask the user for it at all? Just get the system name (the part to the left of the "@" symbol). – Pointy Nov 14 '11 at 19:11
  • You really don't need regex for this. Just check whether there is one and only one at sign in the given string. Also check that there are no line breaks or invalid characters. – Aadit M Shah Nov 14 '11 at 19:12
  • but you're using jquery-validate i think, if it;s you should in your class `class="required email"` – Jorge Nov 14 '11 at 19:14
  • possible duplicate of [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – Jacob Nov 14 '11 at 19:28

4 Answers4

0

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.

James Johnson
  • 45,496
  • 8
  • 73
  • 110
0

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
Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69
Ray K
  • 1,452
  • 10
  • 17
0

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.

scessor
  • 15,995
  • 4
  • 43
  • 54
0

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('@'));
david
  • 4,218
  • 3
  • 23
  • 25