SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation with reg exp

    I'm working with an example from page 119 of SitePoints JS Anthology..

    I have a form control with a class of "req"
    HTML Code:
    <input type="text" class="text" id="password" name="password" class="req">
    and I'm using this code -- right from JSA but the alert doesn't throw
    HTML Code:
    function validateForm(){
    	
    	var elements = document.forms[0].elements;
    	var emailPattern = /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
    	
    	for(var i = 0; i<elements.length; i++){
    		
    		if(/(^| )req( | $)/.test(elements[i].className)){
    			alert("message here");
    			return false;
    		}
    	
    	}
    	return true;
    }
    I think the reg exp is causing this.. at this point i'm just testing the script. Isn't the script right now saying:
    if there is a class name of req, alert.
    Last edited by tictike; Aug 3, 2008 at 21:21. Reason: edit

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You're checking if it's the start of the string or a space before the word, and if there's a space or the end of the string after the word.

    Try instead /(^| )req( |$)/

    or even better, make it more explicit
    /(^|\s)req(\s|$)/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No go. Can you please look at this link?

    http://www.chris-rawlins.com/css/forms/form/form.html

    Just change "html" to "js" to see the javascript.

    At this point I'm just testing the script, I just want an alert if "req" is a class value.

  4. #4
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I figured it out. It's an embarrassing error on my part.


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
  •