PDA

View Full Version : accessing form elements


Matt Pearson
08-30-2002, 05:22 PM
Hi there,

I've got a function that I've used a lot in the past (and got out of book anyway) so I'm fairly sure is bug free

thing is, when I call this function on a form element using this syntax:

validEmail(thisform.elementname.value)

it works, but If I try this:

validEmail(thisform.elements[1].value)

or this:

validEmail(thisform.elements[elementname].value)

I get errors :(

when I do alert(thisform.elements[1].value) the result returned looks fine

the error message I get is *allways*:

"Email.indexOf is not a function."

here's the function I'm using:

function validEmail(Email) {

Email=Email.toString();
invalidChars = " /:,;"

if (Email == "") {
return false
}

for (i=0;i<invalidChars.length; i++) { badChar = invalidChars.charAt(i)
if (Email.indexOf(badChar,0) > -1) {
return false
}
}

atPos=Email.indexOf("@",1)
if (atPos == -1){
return false
}

if (Email.indexOf("@",atPos+1) != -1) {
return false
}

periodPos = Email.indexOf(".",atPos)
if (periodPos == -1) {
return false }

if (periodPos+3 > Email.length) {
return false
}
return true
}

ShriekForth
08-30-2002, 06:20 PM
It is probably not an error with the function. The function simply expects a string. I am guessing the problem lies in the way you are passing the value. For kicks, I would put an alert in as the first line of the validateEmail function and see what Email really is. It the error makes it appear that Email did not get converted into a string properly.

validEmail(Email) {
alert(Email)
....

I always start with the basics, make sure that the form name is correct, that the field name is correct, and that it does have a value. The fucntion seems to work fine if I try any of the ways you have listed.

ShriekForth

martin_narg
08-30-2002, 07:31 PM
i'd be inclined to use a regular expression for your test of invalid chars, and string comparisons/manipulations for the rest, i've included an email checker that I wrote ages ago, and use all the time. Hope it is helpful.

function checkEmail(str) {

var isOK = true;
rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/ /*invalid char list */
if( rExp.test(str) )
isOK = false;
if( str.indexOf('.') == -1 || str.indexOf('@') == -1 ) /* check for . and @ symbols */
isOK = false;
if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 ) /* check gtld length */
isOK = false;
if( str.slice(0,str.indexOf('@')).length < 1 ) /* check name length */
isOK = false;
if( str.slice(str.indexOf('@')+1,str.lastIndexOf('.')).length < 1 ) /* check domain length */
isOK = false;

if( !isOK ) {
alert( "invalid email address" );
}
return isOK;
}


pass the function the email address and it will return true/false (accompanied by an error message if needed).

i generally use it in my <form></form> tags using an onSubmit event:

<form name ="myForm" method="post" action="myFormHandler.html" onSubmit="return checkEmail(this.emailInputName.value)">


m_n

Matt Pearson
08-30-2002, 08:47 PM
hi everyone,
thanks for your help!
I realise reg exp is a much better way of going about things, but I'm curious as to why the function works when I call it one way, and doesn't when I call it another:(

I have tried using alert to find out what the string looks like when it makes it to the function, but the strings *look* identical whichever way the function is called.

I've tried using toString, to ensure that the info the function gets is processed as a string, but no joy.

is there a way of detecting what type a variable is?

thanks again

Matt

Matt Pearson
08-30-2002, 09:07 PM
I've just tried testing Email.length in the function

when it works(name method) the length is the length of the string.

when it doesn't work (number method) the length is returned as "undefined" even though the string looks the same as it did in the other method.

hmmmmm.........

ShriekForth
08-30-2002, 09:18 PM
typeof(Email) will return

Undefined,
Null,
Boolean,
Number, or
String.


If Email is not a String, then the indexOf function will not be available, and that will produce an error.

ShriekForth

Matt Pearson
08-30-2002, 10:20 PM
Thanks ShriekForth, I'll remember that one for future debugging ;)

typeof(Email) returned "string" for both methods of calling the function.

I'm assuming the length of the string being "undefined" is what's causing the indexOf error.

how can a string end up with an undefined length?

thanks again

Matt

ShriekForth
08-30-2002, 10:35 PM
Could the string be empty? Even then I would think that it would return at least a 0 if it were a string object. It is definied to return an integer.

if (Email == "") {

return false

}

should keep it from checking the indexOf of the string if it is empty.

I'm not sure where else to look, guess I would have to see the full page.

ShriekForth

adios
08-30-2002, 11:50 PM
Something else is clearly amiss here. Could you post a (non-working) example - ready-to-paste - that we could play with? I'd like to see this.

whammy
08-31-2002, 12:17 AM
This strangely sounds like the problem I was having with VBScript a couple of days ago (not with validating an email address, but similar!).

Like an idiot, I didn't think to use a pure regex (I should know better). When I did, it worked - and it took me about 10 seconds to write it. Go figure.

If I were you i'd just use a regular expression to validate the email ((and if you really, REALLY want to validate for browser versions earlier than 4.0 (which I don't care about anymore)) use some browser checking code and a loose alternative that checks for @ and . in the string).

Here's a simple one (kind of loose, but it works - and if someone wants to fool your validation, they can anyway - not to mention most of the "really great" email validation regex's I've seen that were more "strict" fail on some weird *valid* email addresses that were entered!):

^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,}$

Matt Pearson
08-31-2002, 04:18 PM
Hi everyone,
well, I finally fixed it, thanks for all you help chasing the errors down!

basically, it was a NS only error, where if I tried to access a element using a array key held in a string like this:

String="form[firstname]"
if (!notempty(thisform.elements[String].value)){
alert("Please enter your first name.")
return false
}


*anywhere* in the function then the string passed to the validEmail function would have a length of "undefined" no matter how the element you were testing the email function on was addressed.

this page shows an example of what was happening, the middle button was the method that wasn't working (in Netscape)

http://www.horse-balloon.co.uk/test1.htm

I solved the problem by storing the names of the elements in an array instead. like this:

Elementnames= new Array("form[firstname]",
"form[lastname]",
"form[title]")
if (!notempty(thisform.elements[Elementnames[0]].value)){
alert("Please enter your first name.")
return false
}

Just shows what can go wrong passing things between functions. I have learned my lesson for next time ;0)

Matt