0

I trying to create this form that only allows people to submit an email that is using a @msu.edu email. I have the alert that is coming up when it's not a msu.edu email, but when I use an email like [email protected] its not submitting the form.

function checkMail() { var email = document.validation.email.value.toLowerCase(); var filter = /^([\w-]+(?:\.[\w-]+)*)@msu.edu/i; var result = filter.test(email); alert('Valid email found: ' + result); return result; } 

4 Answers 4

3

You need to escape the . there.

Correct code:

function checkMail() { var email = document.validation.email.value.toLowerCase(); var filter = /^([\w-]+(?:\.[\w-]+)*)@msu\.edu/i; // ^-- escape this dot var result = filter.test(email); alert('Valid email found: ' + result); return result; } 

Demo

Sign up to request clarification or add additional context in comments.

6 Comments

I dont get what you are saying because its still not working. If it passes the test then it should go on to my php page that i have created which sends it to my email
What Gaurang is saying is that you were allowing @msukedu, @msupedu, etc. as valid — which we assume isn't.
@user3240208 I created a test script and it works in there
After you enter the right email. The pop up box should not come up and the php page should load next
So, if you don't want the popup to appear if the user entered right email, you can wrap it in if like if (result === true) postPHPrequest(); else alert("Invalid email entered");
|
1

Format in just like this... var filter = /^([\w-]+(?:\.[\w-]+)*)@msu\.edu/i; as per Gaurang said. It works.

Comments

0

why not try this

bool IsValidEmail(string email) { try { var addr = new System.Net.Mail.MailAddress(email); return true; } catch { return false; } } 

Comments

0

This will do it

put this function onclick event of button and change getelementById's Id with your TextBox Id and this will give you alert "msu.edu"

 function checkMail() { var email = document.getElementById("email").value.toLowerCase(); var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\ [[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; ; if (regex.test(email)) { var temp = email.substring(email.lastIndexOf("@")+1); if (temp != "msu.edu") { alert("Enter correct email"); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.