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:
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:
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?
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; }
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; } }
Comment