Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Thanked 0 Times in 0 Posts

    Must enter either a phone number or address

    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


  • #2
    Regular Coder
    Join Date
    Sep 2007
    AZ, USA
    Thanked 46 Times in 46 Posts
    I apologize in advance for the less than subtle google links littered in the below paragraph .

    This is called javascript validation. There are plently of tutorials on google. You can also specifically search for javascript phone number validation (this one 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 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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    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
    Last edited by Philip M; 08-09-2008 at 10:53 AM.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts