...

View Full Version : Check email function not working...?



gerb
06-05-2006, 12:37 AM
Hi,

I'm using this two part validation method which check a number of fields with one main function and multiple minor functions. At the moment, I'm trying to validate an email address.

The first bit of code I know works because other fields (commented below) are checked correctly. This is the main function:


function checkForm(formname, submitbutton)
{
var errors = '';
//errors += checkText(formname, 'Name', 'Please enter your name');
errors += checkEmail(formname, 'Email', 'Please enter your email address');
checkThisForm(formname, submitbutton, errors);
}

So, it appears that this is the bit of code that isn't working:


function checkEmail(formname, textboxname, displaytext)
{
var valid = "^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$";
var localerror = '';
if(!isValid(Trim(eval('document.'+formname+'.'+textboxname+'.value')), valid))
{
localerror = '- '+displaytext+'\n';
}
else localerror = '';
return localerror;
}

It's either a problem with line 3 or line 5 as far as I can see. Can someone help me by pointing out what's wrong with the second part of the code?

Thanks...

Kravvitz
06-05-2006, 02:09 AM
Please show us the code for isValid() and Trim().

Don't use eval() -- you don't need it. Change

eval('document.'+formname+'.'+textboxname+'.value')to

document.forms[formname].elements[textboxname].value

gerb
06-05-2006, 02:16 AM
Thanks - hope you can help. Here's the code for those functions:


function Trim(s)
{
while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
{
s = s.substring(1,s.length);
}
while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
{
s = s.substring(0,s.length-1);
}
return s;
}

function isValid(string,allowed)
{
// var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // define valid characters
for (var i=0; i< string.length; i++)
{
if (allowed.indexOf(string.charAt(i)) == -1) return false;
}
return true;
}

gerb
06-06-2006, 12:41 AM
Anyone?

Kravvitz
06-06-2006, 02:13 AM
Your code doesn't make sense -- you're passing a RegExp to isValid(), but it only checks to see if a character is in one string that is not in a string containing all allowed characters.

Also, Trim() could be written much simpler if you just used a RegExp.

gerb
06-06-2006, 09:39 AM
Yep - not my code. It's a script I've found on the internet and, with my limited knowledge am trying to adapt...

So I assume that there's no easy fix?

Kravvitz
06-06-2006, 09:34 PM
Ah. Trying to combine scripts without understanding how they work.

Easy is relative. Someone who is experienced, like I am, could rewrite the code to do what you want without much trouble. On the other hand, someone who doesn't know JavaScript very well would have significantly more difficulty.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum