waps

Jun 19th, 2002, 08:33 PM

Hi people,

I have found this script for validating an email address entered into a form field.

Yet it seems in complete. When you enter e.g. "[email protected]". It works. But when you put "[email protected]" it still works. So it needs a bit adding to it. In the script it checks to make sure there are characters after the '.', but it needs a section to check that there are characters before the '.' also. I have tried figuring it out, but to no evail. Any one help me with this little addition.

Thanks in advance

:D :D

:confused: oops, forgot the code the first time round when posting, here it is !!!!!

function validEmail(email) {

invalidChars = " /:,;"

if (email == "") { // Cannot be empty

return false

}

for (i=0; i<invalidChars.length; i++) { // Does it contain any invalid characters

badChar = invalidChars.charAt(i)

if (email.indexOf(badChar,0) > -1) {

return false

}

}

atPos = email.indexOf("@",1) // There must be at least one '@' symbol

if (atPos == -1) {

return false

}

if (email.indexOf("@",atPos+1) != -1) { // And only one '@' symbol

return false

}

periodPos = email.indexOf(".",atPos) // at least one '.' after the '@'

if (periodPos == -1) {

return false

}

if (periodPos+3 > email.length) { // must be at least 2 characters after the '.'

return false

}

return true

}

I have found this script for validating an email address entered into a form field.

Yet it seems in complete. When you enter e.g. "[email protected]". It works. But when you put "[email protected]" it still works. So it needs a bit adding to it. In the script it checks to make sure there are characters after the '.', but it needs a section to check that there are characters before the '.' also. I have tried figuring it out, but to no evail. Any one help me with this little addition.

Thanks in advance

:D :D

:confused: oops, forgot the code the first time round when posting, here it is !!!!!

function validEmail(email) {

invalidChars = " /:,;"

if (email == "") { // Cannot be empty

return false

}

for (i=0; i<invalidChars.length; i++) { // Does it contain any invalid characters

badChar = invalidChars.charAt(i)

if (email.indexOf(badChar,0) > -1) {

return false

}

}

atPos = email.indexOf("@",1) // There must be at least one '@' symbol

if (atPos == -1) {

return false

}

if (email.indexOf("@",atPos+1) != -1) { // And only one '@' symbol

return false

}

periodPos = email.indexOf(".",atPos) // at least one '.' after the '@'

if (periodPos == -1) {

return false

}

if (periodPos+3 > email.length) { // must be at least 2 characters after the '.'

return false

}

return true

}