I'm trying to validate a mobile phone number on my registration but I'm only concerned with validating the number if the person registering is from Ireland.<BR>When I use the followong code, the validation works but the country selected (ie.Ireland) doesn't get saved in the database.<BR><BR>if(form.cboParentLocation.selecte dIndex = '564') <BR> { <BR> var mobilePattern = new RegExp("(^35386| 35387| 35385) *[0-9$]{7}"); <BR> if (!mobilePattern.test(form.txtMobileNumber.value) && form.txtMobileNumber.value.length>0)<BR> {<BR> alert('Your mobile number does not look valid.
Please enter it in the form: (country code) + (mobile number) - (initial zero)
eg. for Ireland: "353861234567"');<BR> form.txtMobileNumber.focus(); <BR> return(false); <BR> } <BR> } <BR><BR>However if I use a double "equals" sign instead of a single one on the first line, the country is saved but the validation doesn't work!<BR><BR>
Let's review your script:<BR><BR>if(form.cboParentLocation.selectedI ndex='564')<BR><BR>::WHOA::<BR><BR>' ;=' is an ASSIGMENT operator. You CANNOT use it like that here. '=' will assign '564' to the selectedIndex, which is obviously not desirable.<BR><BR>You *MUST* use '==' (equals or is equal to). The opposite is '!='.<BR><BR>You didn't say if you were getting an error or not, but I don't like to use of "form.whateverfield.value". Is 'form' the name of your form?<BR><BR>I always use something like:<BR>document.forms[0].frmField.value.<BR><BR>Or, more accurately, and cleaner:<BR><BR>with(document.forms[0])<BR>{<BR>code goes here<BR>}<BR><BR>etc.<BR><BR>
Thank You for your help!...I am new to this as I'm sure you have gathered!<BR><BR>Yes 'form' is the name of my form in this case. I wasn't getting any error message, it just didn't validate properly.<BR><BR>I'm not too sure about what you meant in your reply.....<BR><BR>should my code read:<BR><BR>with(document.form["564"])<BR> { <BR> var mobilePattern = new RegExp("(^35386| 35387| 35385) *[0-9$]{7}"); <BR> if (!mobilePattern.test(form.txtMobileNumber.value) && form.txtMobileNumber.value.length>0)<BR> {<BR> alert('Your mobile number does not look valid.
Please enter it in the form: (country code) + (mobile number) - (initial zero)
eg. for Ireland: "353861234567"');<BR> form.txtMobileNumber.focus(); <BR> return(false); <BR> } <BR> } <BR>
'form' is probably a reserved word. I'd ditch it.<BR><BR>'document' is the top level of your object. It refers to the entire HTML page.<BR><BR>'forms[0]' is a reference to your form in the 'document'. It's an array reference and '0' is the first form. If you had two forms on your page, forms[0] would be the first, forms[1] would be the second form.<BR><BR>You would *not* use '564', because that would be trying to reference form object 565. Yes?<BR><BR>Now, you can use a form name there instead = it's case sensitive - but stay away from reserved words. Try:<BR><BR><form name="myForm" ... ... ... ><BR><BR>And then:<BR><BR>document.myForm<BR><BR>Now, the next part is the next level down. We've gone from document to form to ... form elements. This is the bit you are trying to validate. Therefore:<BR><BR>'document.myForm.phonenumbe r' would access a form element called 'phonenumber'. This is case sensitive also, so be wary.<BR><BR>Now, this is fine and dandy, but what part of the 'phonenumber' object are you trying to reference? That's right ... it's value.<BR><BR>document.myForm.phonenumber.value<BR ><BR>OR<BR><BR>document.forms[0].elements["phonenumber"].value<BR><BR>Or, if youe phonenumber was the 3rd element on the page (and I never do this myself):<BR><BR>document.forms[0].elements[2].value (the arrays start at zero). That way is confusing.<BR><BR>Get rid of the 'form' name and see how that goes .. but you understand the rest of it, right?
Thank you very much!<BR><BR>That works fine now........ but only in Internet Explorer!!<BR>The ".value" attribute does not seem to be recognised in Netscape..........or could it be something else?<BR>
Bookmarks