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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

email validation

Status
Not open for further replies.

slowfish

Programmer
Aug 21, 2009
28
GB
Hi

I have an email field in my form where javascript email validation works fine on submit. I've recently added another email field but some reason the email validation now doesn't work.

Any ideas?
 
Yes
There is something wrong in your code.

That is as much help as anyone can give unless you provide examples of the code & how it is failing.
 
Here's the code. The first email field is ref1email and the second field is ref2email. If ref1email is not a valid email address the alert pops up. If ref1email address amended so correct, form submitted and passes through without any alert that ref2email is invalid.

Code:
    if (document.ff.ref1email.value != "")
    {
    var str = new String(document.ff.ref1email.value);
    var isOK = true;
    rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
    if( rExp.test(str) )
    isOK = false;
    if( str.indexOf('.') == -1 || str.indexOf('\@') == -1 )
    isOK = false;
    if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
    if( str.slice(0,str.indexOf('\@')).length < 1 )
    isOK = false;
    if( str.slice(str.indexOf('\@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

    if( !isOK )
    alert( "Invalid email address - Reference One" );

    return isOK;
    }


    if (document.ff.ref1email.value != "")
    {
    var str = new String(document.ff.ref1email.value);
    var isOK = true;
    rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
    if( rExp.test(str) )
    isOK = false;
    if( str.indexOf('.') == -1 || str.indexOf('\@') == -1 )
    isOK = false;
    if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
    if( str.slice(0,str.indexOf('\@')).length < 1 )
    isOK = false;
    if( str.slice(str.indexOf('\@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

    if( !isOK )
    alert( "Invalid email address - Reference One" );

    return isOK;
    }
 
It looks like they both refer to the same form.

_________________
Bob Rashkin
 
Yes, there are two email fields in the same form (ff)
 
Apparently, one is named ref1email and the other is named, well, ref1email.

_________________
Bob Rashkin
 
Apologies, this is the actual code I'm using. Field names are different, both fields are in form "ff".

Code:
    if (document.ff.ref1email.value != "")
    {
    var str = new String(document.ff.ref1email.value);
    var isOK = true;
    rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
    if( rExp.test(str) )
    isOK = false;
    if( str.indexOf('.') == -1 || str.indexOf('\@') == -1 )
    isOK = false;
    if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
    if( str.slice(0,str.indexOf('\@')).length < 1 )
    isOK = false;
    if( str.slice(str.indexOf('\@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

    if( !isOK )
    alert( "Invalid email address - Reference One" );

    return isOK;
    }


    if (document.ff.ref2email.value != "")
    {
    var str = new String(document.ff.ref2email.value);
    var isOK = true;
    rExp = /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/
    if( rExp.test(str) )
    isOK = false;
    if( str.indexOf('.') == -1 || str.indexOf('\@') == -1 )
    isOK = false;
    if( str.slice(str.lastIndexOf('.')+1,str.length).length < 2 )
    isOK = false;
    if( str.slice(0,str.indexOf('\@')).length < 1 )
    isOK = false;
    if( str.slice(str.indexOf('\@')+1,str.lastIndexOf('.')).length < 1 )
    isOK = false;

    if( !isOK )
    alert( "Invalid email address - Reference Two" );

    return isOK;
    }
 
And you say 1 works and the other doesn't. That's curious. I suggest you follow feherke's suggestion and pass the field name as an argument to a single function.


_________________
Bob Rashkin
 
[tt] if( !isOK ) [red]{[/red]
alert( "Invalid email address - Reference One" );

return isOK;
[red]}[/red]
[/tt]
When you "return", it is over. And you tolerate yourself to produce such thing?
 
And you say 1 works and the other doesn't. That's curious. I suggest you follow feherke's suggestion and pass the field name as an argument to a single function.

Can you help as to how I would do this?

tsuji - not sure I understand what you mean by "And you tolerate yourself to produce such thing?"

Apologies all, quite new to javascript and the code I used was just copied after a google search.

Thanks
 
Hi

JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]return[/b] [teal]![/teal] [teal]([/teal]
    what [teal]==[/teal] [green][i]""[/i][/green]
    [teal]||[/teal] [fuchsia]/[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/[/fuchsia][teal].[/teal][COLOR=darkgoldenrod]test[/color][teal]([/teal]what[teal])[/teal]
    [teal]||[/teal] what[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal][green][i]'.'[/i][/green][teal])[/teal] [teal]==[/teal] [teal]-[/teal][purple]1[/purple]
    [teal]||[/teal] what[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal][green][i]'@'[/i][/green][teal])[/teal] [teal]==[/teal] [teal]-[/teal][purple]1[/purple]
    [teal]||[/teal] what[teal].[/teal][COLOR=darkgoldenrod]slice[/color][teal]([/teal]what[teal].[/teal][COLOR=darkgoldenrod]lastIndexOf[/color][teal]([/teal][green][i]'.'[/i][/green][teal])+[/teal][purple]1[/purple][teal],[/teal]what[teal].[/teal]length[teal]).[/teal]length [teal]<[/teal] [purple]2[/purple]
    [teal]||[/teal] what[teal].[/teal][COLOR=darkgoldenrod]slice[/color][teal]([/teal][purple]0[/purple][teal],[/teal]what[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal][green][i]'@'[/i][/green][teal])).[/teal]length [teal]<[/teal] [purple]1[/purple]
    [teal]||[/teal] what[teal].[/teal][COLOR=darkgoldenrod]slice[/color][teal]([/teal]what[teal].[/teal][COLOR=darkgoldenrod]indexOf[/color][teal]([/teal][green][i]'@'[/i][/green][teal])+[/teal][purple]1[/purple][teal],[/teal]what[teal].[/teal][COLOR=darkgoldenrod]lastIndexOf[/color][teal]([/teal][green][i]'.'[/i][/green][teal])).[/teal]length [teal]<[/teal] [purple]1[/purple]
  [teal])[/teal]
[teal]}[/teal]

[b]function[/b] [COLOR=darkgoldenrod]validate[/color][teal]([/teal]form[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] error[teal]=[/teal]
    [teal]([/teal][COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]form[teal].[/teal]ref1email[teal].[/teal]value[teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid email address - Reference One'[/i][/green][teal])[/teal]
    [teal]+[/teal] [teal]([/teal][COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]form[teal].[/teal]ref2email[teal].[/teal]value[teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid email address - Reference Two'[/i][/green][teal])[/teal]

  [b]if[/b] [teal]([/teal]error[teal])[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'Error occurred :'[/i][/green][teal]+[/teal]error[teal])[/teal]

  [b]return[/b] [teal]![/teal]error
[teal]}[/teal]
Code:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onsubmit[/maroon][teal]=[/teal][green][i]"return validate(this)"[/i][/green][b]>[/b]
[gray]<!-- ... -->[/gray]
[b]</form>[/b]
Note that your validation code will accept for example _@_.__ and 1@2.34 as valid e-mail addresses.

Feherke.
 
Thanks feherke.

What about if I have other fields in the form that I need to check for? Can I put these in the validate(form) function too?
 
Hi

Yes.
[ul]
[li][highlight]For more e-mail addresses, just add more [tt]checkemail()[/tt] calls.[/highlight][/li]
[li][highlight pink]For other complex validations, put the code in other functions and call them.[/highlight][/li]
[li][highlight paleturquoise]For simple validations, you can add the conditions directly.[/highlight][/li]
[/ul]
Code:
[b]function[/b] [COLOR=darkgoldenrod]validate[/color][teal]([/teal]form[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] error[teal]=[/teal]
    [teal]([/teal][COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]form[teal].[/teal]ref1email[teal].[/teal]value[teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid email address - Reference One'[/i][/green][teal])[/teal]
    [teal]+[/teal] [teal]([/teal][COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]form[teal].[/teal]ref2email[teal].[/teal]value[teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid email address - Reference Two'[/i][/green][teal])[/teal]
    [highlight][teal]+[/teal] [teal]([/teal][COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]form[teal].[/teal]ref3email[teal].[/teal]value[teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid email address - Reference Three'[/i][/green][teal])[/teal][/highlight]
    [highlight pink][teal]+[/teal] [teal]([/teal][COLOR=darkgoldenrod]checkssn[/color][teal]([/teal]form[teal].[/teal]ssn[teal].[/teal]value[teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid social security number - Wrong format'[/i][/green][teal])[/teal][/highlight]
    [highlight paleturquoise][teal]+[/teal] [teal]([/teal]form[teal].[/teal]thename[teal].[/teal]value[teal]?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid name - Can not be empty'[/i][/green][teal])[/teal][/highlight]
    [highlight paleturquoise][teal]+[/teal] [teal](![/teal]form[teal].[/teal]age[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/\D/[/fuchsia][teal])?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid age - Contains non-digits'[/i][/green][teal])[/teal][/highlight]
    [highlight paleturquoise][teal]+[/teal] [teal]([/teal]form[teal].[/teal]address[teal].[/teal]value[teal].[/teal]length[teal]>=[/teal][purple]10[/purple][teal]?[/teal][green][i]''[/i][/green][teal]:[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Invalid address - Too short'[/i][/green][teal])[/teal][/highlight]

  [b]if[/b] [teal]([/teal]error[teal])[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'Error occurred :'[/i][/green][teal]+[/teal]error[teal])[/teal]

  [b]return[/b] [teal]![/teal]error
[teal]}[/teal]


Feherke.
 
Still can't get this to work...

Code:
function checkemail(what)
{
  return ! (
    what == ""
    || /[!\"£$%\^&*()-+=<>,\'#?\\|¬`\/\[\]]/.test(what)
    || what.indexOf('.') == -1
    || what.indexOf('@') == -1
    || what.slice(what.lastIndexOf('.')+1,what.length).length < 2
    || what.slice(0,what.indexOf('@')).length < 1
    || what.slice(what.indexOf('@')+1,what.lastIndexOf('.')).length < 1
  )
}


  var error=
    (checkemail(document.ff.staffsupporteremail.value))

  if (error) {

  alert("Error occurred");

  return false;

  }
 
Hi

You reversed the logic. checkmail() returns [tt]true[/tt] if the parameter is syntactically valid e-mail address and [tt]false[/tt] if not.

You have to negate the returned value :
Code:
[b]var[/b] error[teal]=[highlight]![/highlight][/teal][COLOR=darkgoldenrod]checkemail[/color][teal]([/teal]document[teal].[/teal]ff[teal].[/teal]staffsupporteremail[teal].[/teal]value[teal])[/teal]
Note that the outer parenthesis was needed to delimit the ternary operator's operands. As you removed the ternary operator there is no need for them.


Feherke.
 
could I send the code to you so you can take a look?

many thanks for all your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top