Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to understand regular expressions and javascript

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
I am trying to understand regular expressions and javascript. I wanted to try and validate an email address.

I read a tutorial on using reqular expressions and they had a sample validating an SSN

Link

I took that example and incorporated an expression for an email address I got from here.


I could not get it to work and I have supplied my sample below. I appreciate any help. Thanks Howard


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<html>
<head>
<title>ssn Checker</title>
<script type="text/javascript">
var RE_EMAIL = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/;


function checkEmail(email){
if (RE_EMAIL.test(email)) {
alert("VALID EMAIL");
} else {
alert("INVALID EMAIL");
}
}
</script>
</head>
<body>
<form onsubmit="return false;">
<input type="text" name="email" size="20">
<input type="button" value="Check"
onclick="checkEmail(this.form.email.value);">
</form>
</body>
</html>
 
Hi

That looks abit too restrictive :
[ul]
[li]only accepts uppercase letters[/li]
[li]only accepts top level domains with length between 2 and 4 characters[/li]
[/ul]
Code:
[b]var[/b] RE_EMAIL [teal]=[/teal] [fuchsia]/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,[highlight]6[/highlight]}\b/[highlight]i[/highlight][/fuchsia][teal];[/teal]

Feherke.
 
Thanks that work

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<html>
<head>
<title>ssn Checker</title>
<script type="text/javascript">
var RE_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;
var RE_EMAIL = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;


function checkEmail(email){
if (RE_EMAIL.test(email)) {
alert("VALID EMAIL");
} else {
alert("INVALID EMAIL");
}
}
</script>
</head>
<body>
<form onsubmit="return false;">
<input type="text" name="email" size="20">
<input type="button" value="Check"
onclick="checkEmail(this.form.email.value);">
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top