SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basic regExp javaScript validation function

    Hi

    I am attempting to write a basic validation function that will check a field value that is passed in against a regExp and return true or false:

    Code:
    function isDate(field) {
    	var validatePattern = new RegExp("\\d{2}/\\d{2}/\\d{4}");
    	
    	if (validatePattern.test(field)) {
    		return true;
    	} else {
    		return false;
    	}
    }
    I can't spot the syntax error, this function always returns false for valid pattern.

    Thanks

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't .test expect a string rather than a RegExp object?

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm unsure, haven't used regex in javaScript before.

    Thanks

  4. #4
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well try this instead:
    Code:
    function isDate(field) {
    	if ("/\d{2}\/\d{2}\/\d{4}/".test(field)) {
    		return true;
    	} else {
    		return false;
    	}
    }

  5. #5
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi - no luck with that syntax, JavaScript console in Firefox:
    Error: "/d{2}/d{2}/d{4}/".test is not a function

    Thanks for the Suggestion.

  6. #6
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That function does work. I think there must be somehow wrong with how you are passing in the field parameter. But if you stick in a line like
    alert(isDate('01/02/2006'));
    you will see that the alert will say true.

    BTW, you can clean up your code just by saying
    return validatePattern.test(field);
    instead of all that if/else stuff.

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    regexp

    Error: "/d{2}/d{2}/d{4}/".test is not a function
    The braces are setting limits on what? Try
    Code:
    if ("/[\d]{2}\/[\d]{2}\/[\d]{4}/".test(field)) {
    EDIT - Although I've always (so far) used braces to limit the acceptable range of what is in the brackets.They are in fact not needed for this example. It should indeed be working. I was assuming the missing backslash escapes for the forward slashes was an effect of SPs vBulletin not showing them because the snippet wasn't put inside BB tags, but maybe you forgot them in your code?


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
  •