By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Validating a field that isn't required in javascript

rush it
P: 18
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:

Expand|Select|Wrap|Line Numbers
  2. var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
  4.     if (regexObj.test(document.forms["form1"]["phone"].value)) {
  6.     } else {
  7.         alert("Please enter Phone Number as:  555-555-5555");
  8.         return false;
  9.     }
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:

Expand|Select|Wrap|Line Numbers
  2. if ( >0) {
  6." ")
  10.      if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (j-i>1) &&(len-jj >=2) && (len-jj<=3)) {
  11.      }
  12.      else {
  13.          alert("Please enter your email address as: (or .net, .org, etc.)\n" );
  15.         return false;
  16.      }
  17. }
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?
Nov 7 '11 #1

✓ answered by Dormilich

maybe something along
Expand|Select|Wrap|Line Numbers
  1. if (form_field.value.length)
  2.     // do validation

Share this Question
Share on Google+
4 Replies

Expert Mod 5K+
P: 8,639
maybe something along
Expand|Select|Wrap|Line Numbers
  1. if (form_field.value.length)
  2.     // do validation
Nov 7 '11 #2

rush it
P: 18
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.
Nov 8 '11 #3

Expert Mod 10K+
P: 12,430
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.
Nov 8 '11 #4

rush it
P: 18
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.
Nov 8 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.