View Full Version : Must enter either a phone number or address

08-08-2008, 09:45 PM
Please help i am pretty new to this hole java and html think.

I need help coding so that you must enter either a phone number or address


08-09-2008, 12:55 AM
I apologize in advance for the less than subtle google links littered in the below paragraph :D.

This is called javascript validation (http://www.w3schools.com/js/js_form_validation.asp). There are plently of tutorials on google (http://www.google.com/search?hl=en&q=javascript+validation&btnG=Google+Search&aq=f). You can also specifically search for javascript phone number validation (http://www.google.com/search?aq=f&complete=1&hl=en&q=javascript+phone+number+validation&btnG=Search) (this one (http://www.smartwebby.com/DHTML/phone_no_validation.asp) is a nice one). Searching for javascript address validation rarely finds anything, just a bunch of validations for "javascript email address validation", so you could try looking into javascript regular expressions (http://www.google.com/search?hl=en&q=javascript+regular+expressions&btnG=Google+Search&aq=f) and make your own; it should not be too difficult.

And if you ever need help along the way, chuck us a thread! :)

Good luck,
~ binaryWeapon

Philip M
08-09-2008, 10:06 AM
First of all, be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

You want that a certain field contains either a telephone number or a street address.

This cannot really be done as it is too difficult to define what is meant by an an address. Are PO Box numbers allowed? And you do not specify the valid format of a telephone number. Numbers only? How many digits? Are spaces and/or hyphens allowed as separators?

My suggestion is that you include in your form two fields, one for telephone number and the other for address, and then the user must fill in either (or perhaps both).

Then the validation is easy:-

For example:-

if (/^[0-9\s\-]{10,}$/gi.test(phoneNumber.value)) { /// telephone number, only digits, space, hyphen are allowed. Minimum of 10 characters.

Address validation is harder - they come in many different formats. Perhaps best to simply strip out any leading spaces and then check that the field contains either zero or at least so-many (say 10) characters, or a PO box number.

addressField = addressField.replace(/^\s+/,""); // strip leading spaces
if ((addressField.length > 0) && (addressField.length < 10)) { // either 0 or more than 9 characters OK
alert ("You must enter an address in this field");
return false;

If you want I will show you the code to validate a PO Box number.

The Moving Finger writes; and, having writ,
Moves on: nor all your piety nor wit
Shall lure it back to cancel half a line,
Nor all your tears wash out a word of it.

-- Omar Khayyam