www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] About date validation(regexp)

  1. #1
    Join Date
    Sep 2012
    Posts
    3

    resolved [RESOLVED] About date validation(regexp)

    I want to validate whether the date(day) user input is correct format or not, but the code seems wrong, it displays "invalid" all time...


    in js:

    Code:
    function validateCc3() {
    	if (document.loginform.cc3.value == "") {
    		errorMessage(document.loginform.cc3, "can't be blank");
    		return false;
    	}
    	else {
    		var cc3_regexp = /0[1-9]|[12][0-9]|3[01]/;
    		var cc3 = document.loginform.cc1.value;
    		var valid = cc3_regexp.exec(cc1);
    		if (!valid) {
    		errorMessage(document.loginform.cc3, "invalid");
    		return false;
    		}
    		else {
    		removeErrorMessage(document.loginform.cc3);
    		return true; 		}
    	}
    }
    html:

    Code:
    <input class="requiredshort" type="text" id="cc3" name="cc3" maxlength="2" size="4" onblur="validateCc3();">

  2. #2
    Join Date
    May 2010
    Location
    Effingham, IL
    Posts
    87

    Selection Tool

    If you are worried about the correct format, you may want to use a selection option, that way you can handle the format easier, trying to have them input the correct format, may cause some bad data to be entered. Let's face no matter how smart we think we are as developers. The user can always come up with weird scenarios and may end up figuring out a way to enter bad data into your form. Also, in some cases JavaScript is turned off what happens then, do you have a backup plan? You should use a server-side language to do this, so the client can't turn off your validation if you need to use a text field for the date.
    Ryan Condron
    Freelance Web Developer
    http://www.rebelwebdevelopment.com

  3. #3
    Join Date
    Sep 2012
    Posts
    3

    Unhappy

    Quote Originally Posted by rebelweb2007 View Post
    If you are worried about the correct format, you may want to use a selection option, that way you can handle the format easier, trying to have them input the correct format, may cause some bad data to be entered. Let's face no matter how smart we think we are as developers. The user can always come up with weird scenarios and may end up figuring out a way to enter bad data into your form. Also, in some cases JavaScript is turned off what happens then, do you have a backup plan? You should use a server-side language to do this, so the client can't turn off your validation if you need to use a text field for the date.
    really thanks for your suggestions. However, I am doing my homework.. so ... this is what my teacher ask me to do- something about regular expression, SAD...

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Your regular expression seems to be working fine:

    Code:
    function validateCc3()
    {
    	if (/0[1-9]|[12][0-9]|3[01]/.test(document.loginform.cc3.value)) {
    
    		alert('Valid!');
    
    	} else {
    
    		alert('Invalid!');
    
    	}
    }
    In the original code I did see a few inconsistencies, like variable names and what field you are querying:

    Code:
    function validateCc3() {
    	if (document.loginform.cc3.value == "") {
    		errorMessage(document.loginform.cc3, "can't be blank");
    		return false;
    	}
    	else {
    		var cc3_regexp = /0[1-9]|[12][0-9]|3[01]/;
    		var cc3 = document.loginform.cc1.value;
    		var valid = cc3_regexp.exec(cc1);
    		if (!valid) {
    		errorMessage(document.loginform.cc3, "invalid");
    		return false;
    		}
    		else {
    		removeErrorMessage(document.loginform.cc3);
    		return true; 		}
    	}
    }
    Last edited by bionoid; 09-02-2012 at 08:01 AM.
    JavaScript: Learn | Validate | Compact | bionoid

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles