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 ;
}
4
  • 1
    What isn't working properly? The /[^a-z]/i pattern should match any non-alpha character. What input are you using to test? Commented Feb 11, 2012 at 22:02
  • Please read this: stackoverflow.com/questions/46155/… Commented Feb 11, 2012 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 Commented Feb 11, 2012 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 :) Commented Feb 11, 2012 at 22:21

2 Answers 2

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) )

Sign up to request clarification or add additional context in comments.

Comments

-1

How about this.

"^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.