Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
November 26, 2014, 10:25:08 AM

Pages: [1]   Go Down
  Print  
Author Topic: Validating Email Address Regular Expressions???  (Read 3275 times)
JohnFortune
Trekkie
**
Offline Offline

Posts: 19


« on: December 13, 2007, 12:43:14 PM »

Hi,
 I am trying to make this regular express for a email validation work, anyone see what might be the problem? 

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta name="generator" content="HTML Tidy for Linux (vers 1 September 2005), see www.w3.org" />

<script language="javascript">
function checkEmail {
var email = document.getElementById(’emailaddress’);
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert(’Please provide a valid email address’);
email.focus
return false;
}
}
</script>


</head>

<body>

<div>

<form name="emailaddress" id="emailaddress" method="post" action="index.html" onSubmit="return checkEmail();">
<fieldset><legend>What's Your Name</legend>

<table>

<tr>
<td>Email</td>
<td><input class="text" id="emailaddress" name="emailaddress" size="20" /></td>
</tr>

</table>


</fieldset>

<p id="formbuttons"><input type="submit" name="nextb" id="nextb" value=" Next " /> </p>


</form>

</div>

</body>
</html>

Logged
scanman20
Senior Moderator
Über Jedi
*****
Offline Offline

Posts: 1545



WWW
« Reply #1 on: December 13, 2007, 01:20:51 PM »

What error are you getting?
Logged

Even a broken clock is right twice a day.
NotOneBit.com
MCSE - MCSA - MCP
JohnFortune
Trekkie
**
Offline Offline

Posts: 19


« Reply #2 on: December 17, 2007, 10:44:02 AM »

Hi,
Thank for replying.  I am not getting an error, the script just doesn't seem to work, I will give you a few examples below.

If I type in nothing "   " it will still submit.
If I type in "1" it will still submit.
If I type in "not a email" it will still submit.

It doesn't seem like the script is working
The alert is not popping up to tell  my, "Please provide a valid email address".

I have alert(’Please provide a valid email address’); in the script

Any Ideas...

Thanks John Fortune
Logged
Ibanez Player 85
Jabba the Hutt
*****
Offline Offline

Posts: 508


« Reply #3 on: December 17, 2007, 11:24:31 AM »

Hey,

Not sure what the problem is, but here's a snippet of code that I normally use (Note: I only left the e-mail part in it. I normally expand this to run a function validate_required for other fields to check for null values. If you want the full code, let me know):

Code:
/**
 * This function validates the form
 */

function validate_form( thisform ) {

with ( thisform ) {

/**
* E-mail
*/

if ( validate_email ( email, "Please enter a valid e-mail address." ) == false ) {

email.focus();

return false

}

}

} // end function validate_form

/**
 * This function checks for a valid e-mail address
 */
 
function validate_email( field, alerttxt ) {

with ( field ) {

apos = value.indexOf( "@" )

dotpos = value.lastIndexOf( "." )

if ( apos <1 || dotpos-apos < 2 ) {

alert( alerttxt );

return false

} else {

return true

}

}

} // end function validate_email

And then in your form, just add this to the form tag:

Code:
onsubmit="return validate_form(this)"

Also make sure your field names match up with the ones in your JS file.

Hope this helps!
Logged
MariaP
Spacescooter Operator
*****
Offline Offline

Posts: 46


« Reply #4 on: February 15, 2008, 03:12:25 AM »

hi,

it looks like you are using the same id for your form and input elements:

form name="emailaddress" id="emailaddress" method="post" action="index.html" onSubmit="return checkEmail();"

<input class="text" id="emailaddress" name="emailaddress" size="20" />

you might want to change it so your id will be unique
Logged

Pages: [1]   Go Up
  Print  
 
Jump to: