0

Can someone rewrite this javascript. The problem now is that 1 regex is only working( that one from email)

earlier question: link I got advice here but I think I put it not good in my code

function checkform ( form )
{
    var rex = /[^a-z]/i;
    var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!regex.test(form.email.value))
    {
        alert( "Please enter youre valid email address." );
        form.email.focus();
        return false ;
    }

    if (rex.test(form.name.value)) 
    {
        alert( "Please enter your name. no numbers or #$^& things allowed)." );
        form.name.focus();
        return false ;
    }

    return true ;
}
Community
  • 1
  • 1
JochemQuery
  • 1,495
  • 2
  • 26
  • 38
  • 1
    What isn't working properly? The `/[^a-z]/i` pattern should match any non-alpha character. What input are you using to test? – Michael Berkowski Feb 11 '12 at 22:02
  • Please read this: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – cha0site Feb 11 '12 at 22:07
  • You should remove the last + behind that `([a-zA-Z0-9]{2,4})+` tld-block, as it would allow you to basically have any tld suffix larger than 2 characters. But aside from that there should not be a problem. Btw, even though your RegExp should suite most needs, just in case you want to take a look at the _real_ email validation RegExp you may do so at [this page](http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html) – Tharabas Feb 11 '12 at 22:16
  • Thanks for all comments , rarely it is working now. Thrabas i will remove it. cha0site I got my regex from there :) I only turned the name check above the email check and it is rarely working now. Thanks :) – JochemQuery Feb 11 '12 at 22:21

2 Answers2

0

Assuming you want the two form fields to behave the same way, the first one is being tested for negative.

You said the email one works, so perhaps you meant to write if ( ! rex.test(for.name.value) )

Casey Kinsey
  • 1,451
  • 9
  • 16
-1

How about this.

"^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"
Thit Lwin Oo
  • 3,388
  • 3
  • 20
  • 23