-1

I have a form that will ask users to enter their first and last name, and their school email. The script was set to only allow a @msu.edu email, but I want it to be able to take any email with a .edu email.

function send() {
        var regex = /^([\w-]+(?:\.[\w-]+)*)@msu\.edu/i;
        var fname = $("#fname").val();
        var lname = $("#lname").val();
        var email = $("#email").val();
        if(fname != "" && lname != "") {
            if(regex.test(email)) {
                var newUser = new Parse.Object("StartupRegister");
                newUser.save({
                        fname: fname,
                        lname: lname,
                        email: email
                    },{
                        success: function(user) {
                            window.location = "msu_form.php";
                        }, error: function(user, error) {

                        }
                    });
            } else {
                alert("Not an MSU email.");
            }
        } else {
            alert("You must enter your name.");
        }
    }

</script>
DAndre Ealy
  • 53
  • 1
  • 1
  • 7
  • What have you tried to resolve this? Why didn't it work for you? What was the result? What was the expected result? Where are you stuck in specific? – PeeHaa Nov 02 '14 at 18:08

2 Answers2

0

Change the msu part of your regex to (?:[\w]+\.)+ so it looks like:

var regex = /^([\w-]+(?:\.[\w-]+)*)@(?:[\w-]+\.)+edu/i;
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28
-1

Here is a simple algorithm that you can use :

1- you can create simple function to check if the email address is valid or not

2- match the email address from the end against the string ".edu"

real world example :

function validateEmail(email) {
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  if( !emailReg.test( email ) ) {
    return false;
  } else {
    return true;
  }
}

the previous function is from : email-validation-using-jquery

function checkEdu(email){
    return (email.substr(-4) == '.edu');
}

the advantage here is that you have a validateEmail function that you can use in other projects

and the checkEdu function which is specific to this case

note :

substr(begin,length) : starts at the index begin and take length character

substr(-length) : starts from the end and take length character

Community
  • 1
  • 1
ismnoiet
  • 4,129
  • 24
  • 30
  • That `validateEmail` function is wrong. For example, it says `postmaster@smithsonian.museum` is invalid. – icktoofay Nov 02 '14 at 19:30
  • Yes to solve this simple problem just replace {2,4} by {2,10 or any other number that you want and it's okay} – ismnoiet Nov 02 '14 at 20:56
  • That was just the tip of the iceberg of what’s wrong with it. The next minimal example of what’s wrong is that it rejects `john+doe@example.com`. Until you end up with [this regular expression](http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html), it rejects valid email addresses and most likely accepts invalid ones. And even if you use that regular expression, the input needs preprocessing, because the syntax of email addresses is context-free, and fundamentally ***cannot*** be matched by a regular expression. – icktoofay Nov 02 '14 at 21:08
  • i agree with you the fact that we can't match all valid email address – ismnoiet Nov 02 '14 at 21:46
  • Not with regular expressions, but there is a way you can match them. It’s just not going to involve regular expressions. – icktoofay Nov 02 '14 at 22:02
  • We can model this problem with DFA(Deterministic finite automate) – ismnoiet Nov 02 '14 at 22:22
  • No; the `comment` production described on the top of page 11 of RFC 822 cannot be expressed with a DFA. – icktoofay Nov 02 '14 at 22:34