Validating a field that isn't required in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rush it
    New Member
    • Oct 2011
    • 18

    Validating a field that isn't required in javascript

    How can I validate a phone number field that isn't required for a form? I'm well aware of the argument about including fields on a form that aren't required. It's not my call, it's the client's. Anyway, there is a Work Phone field on the form that I'd like to validate for the users who DO fill it in. Of course, I want it to be in US phone number format. Here's what I have for the phone fields that are required:

    Code:
    var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    
    	if (regexObj.test(document.forms["form1"]["phone"].value)) {
        
    	} else {
        	alert("Please enter Phone Number as:  555-555-5555");
        	return false;
    	}
    This works flawlessly for the fields that require it. However, I can't find an edit so if the user doesn't enter it, validation just skips it.

    My email validation script only validates if the user fills it in. It's not a required field either. Here's that code:

    Code:
    if (document.form1.email.value.length >0) {
    	 i=document.form1.email.value.indexOf("@")
    	 j=document.form1.email.value.indexOf(".",i)
    	 k=document.form1.email.value.indexOf(",")
    	 kk=document.form1.email.value.indexOf(" ")
    	 jj=document.form1.email.value.lastIndexOf(".")+1
    	 len=document.form1.email.value.length
    
     	if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (j-i>1) &&(len-jj >=2) && (len-jj<=3)) {
     	}
     	else {
     		alert("Please enter your email address as: yourname@yourisp.com (or .net, .org, etc.)\n" );
    		
    		return false;
     	}
    }
    Again, this works flawlessly. With '>O' at the end of the first line, it only validates if the user filled it in; otherwise it gets skipped. Anyone else run into this?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    maybe something along
    Code:
    if (form_field.value.length)
        // do validation

    Comment

    • rush it
      New Member
      • Oct 2011
      • 18

      #3
      Since what I'm trying to conditionally validate is a phone number using a regular expression, that line doesn't work. It allows the form to submit before it's finished. I'm thinking the regular expression is the problem. Maybe there's not a way around it. I'm new to web design and programming and I've only used regular expressions to validate phone numbers, so I'm going to do look for other validation methods.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I don't understand. Aren't you trying to achieve for the phone number the same thing you did for the e-mail? Just use the same concept.

        Comment

        • rush it
          New Member
          • Oct 2011
          • 18

          #5
          It's easy to talk about 'concepts' when you're a seasoned programmer but when you're not, like me, actual syntax helps - at least the first time around. A co-worker helped me see how to use Dormilich's suggestion in my syntax. I just needed to go one step further. Thanks Dormilich.

          Comment

          Working...