www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Need Appropriate Reg Exp & Drop Down Check

  1. #1
    Join Date
    Oct 2007
    Posts
    167

    resolved [RESOLVED] Need Appropriate Reg Exp & Drop Down Check

    I'm having a couple of issues with my form.

    1. I need a regular expression that allows only alpha chars for a name field. My regular expression is allowing this (John D##). Also my expression isn't allowing spaces between first and last names like this (John Doe).

    Code:
    function checkIllegalChars(theuser_name, error){
    
      var name_exp = /[a-zA-Z]+/;
      
      for(var i=0; i < theuser_name.length; i++)
      {
         the_char = theuser_name.charAt(i);
        
        if (!name_exp.test(the_char))
        {
          error.val=2;
          return false;
        }
    
      }
     return true;  
    }
    2. My form is suppose to make sure that a city is selected from the drop down menu. Here's my code for that:

    Code:
     correct=checkSelOpts(sel_opt, error); 
      
      if (!correct)
      {
        error_mes += errors[error.val] + '<br>';
      }
    ...//else statement is further down...it's not the problem
    
    ...
    function checkSelOpts(city_opts, error){
    
     if (city_opts="none")
     {
       error.val=4;
       return false;
     }
    
    return true;
      
    }
    Now I've checked Firebug and "city_opts" has the value of whatever is selected. So that part is working. But when I hit submit, the form isn't recognizing that a city was indeed selected. It still shows an error message regardless of a city being selected or not. Please help.

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Quote Originally Posted by banksworld1986 View Post
    I'm having a couple of issues with my form.

    1. I need a regular expression that allows only alpha chars for a name field. My regular expression is allowing this (John D##). Also my expression isn't allowing spaces between first and last names like this (John Doe).

    Code:
    function checkIllegalChars(theuser_name, error){
    
      var name_exp = /[a-zA-Z]+/;
      
      for(var i=0; i < theuser_name.length; i++)
      {
         the_char = theuser_name.charAt(i);
        
        if (!name_exp.test(the_char))
        {
          error.val=2;
          return false;
        }
    
      }
     return true;  
    }
    Code:
    function checkIllegalChars(theuser_name, error){
      if(/^\w+( \w+)*$/.test(theuser_name)){
        error.val=2;
        return false;
      }else{
        return true;
      }
    }
    2. My form is suppose to make sure that a city is selected from the drop down menu. Here's my code for that:

    Code:
     correct=checkSelOpts(sel_opt, error); 
      
      if (!correct)
      {
        error_mes += errors[error.val] + '<br>';
      }
    ...//else statement is further down...it's not the problem
    
    ...
    function checkSelOpts(city_opts, error){
    
     if (city_opts="none")
     {
       error.val=4;
       return false;
     }
    
    return true;
      
    }
    Now I've checked Firebug and "city_opts" has the value of whatever is selected. So that part is working. But when I hit submit, the form isn't recognizing that a city was indeed selected. It still shows an error message regardless of a city being selected or not. Please help.
    comparisons use == or === not =
    Code:
     if (city_opts=="none")

  3. #3
    Join Date
    Oct 2007
    Posts
    167
    Quote Originally Posted by scragar View Post
    Code:
    function checkIllegalChars(theuser_name, error){
      if(/^\w+( \w+)*$/.test(theuser_name)){
        error.val=2;
        return false;
      }else{
        return true;
      }
    }

    comparisons use == or === not =
    Code:
     if (city_opts=="none")
    That's it...Thanks scragar. It worked.

Thread Information

Users Browsing this Thread

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

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