SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Pixelateur's Avatar
    Join Date
    Feb 2006
    Location
    Hamburg, Germany
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Help with very simple email validation function

    Hi, I am a Javascript newbie and I'm trying to implement a very simple form validation function, but for some reason it won't work. I must be doing something fundamentally wrong here. Maybe somebody can hint me into the right direction? My code looks like so:

    Code:
    function validate_form ()
    {
    	valid = true;
    	var re = new RegExp(); 
         re.compile("[A-Za-z0-9._-]+@[^.]+\..+"); 
    	if ((document.form.author_name.value == "") 
    	|| (document.form.author_email.value == "") 
    	|| (!re.test(document.form.author_email.value))
    	|| (document.form.author_message.value == ""))
    	{
    		alert ("Please fill in all required fields.");
    		valid = false;
    	}
    	return valid;
    }
    The tricky bit is the email validation using the regular expression. Apart from that it all works.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your regular expression defining can be done much more easily, ie:

    Code:
    var re = /[A-Za-z0-9._-]+@[^.]+\..+/;
    I'm not sure that's the best regular expression to use though. Try

    Code:
    var re = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;
    which I've carefully copied from p222 of Nickolas Zakas' book: Professional Javascript for Web Developers.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I must be doing something fundamentally wrong here.
    Then simplify the regular expression to "tom", and see if you can get it to work.

    for some reason it won't work.
    Saying that is a big no-no when you post to programming forums--it provides no information. Always post what you expect to happen and what actually happens.

    More generally, you rarely need to use the RegExp constructor in js. You use it when out of necessity you've used a string variable to create your regex, e.g.

    var str = document.getElementById("myTextbox").value + "[0-9]*";

    Since the RegExp() constructor takes a string, you can then do this:

    var regex = new RegExp(regexStr);

    More commonly, you use a "regex literal". You already know that when you create a string you specify a string by putting quotes around it, e.g.

    var str = "hello";

    What's on the right is called a "string literal".

    Similarly, to specify a "regex literal", you put slashes around a regex, e.g.

    /abc[0-9]*/

    A regex literal puts js on notice that what is between the slashes should be interpreted as a regex. In a regex certain characters have special meanings, and the slashes tell js to start using those special meanings.

    In your case, you can simply write:
    Code:
    var re = /[A-Za-z0-9._-]+@[^.]+\..+/;

  4. #4
    SitePoint Evangelist Pixelateur's Avatar
    Join Date
    Feb 2006
    Location
    Hamburg, Germany
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by 7stud View Post
    Saying that is a big no-no when you post to programming forums--it provides no information. Always post what you expect to happen and what actually happens.
    You are so right... But I've been through an agony of hit and miss before finally posting on this board. My problem is that I don't normally use Javascript. I just needed it in this particular case to validate the user input for my contact form.

    By the way, the reason I don't really like Javascript is that it doesn't provide feedback in the way that other programming languages do. Or IS there some sort of error log to check why my code doesn't work?

    Thanks a lot for your help. The code works great and you'll be able to see it in action soon on my website. Cheers!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •