Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Easy request

  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Easy request

    Man, I haven't written any js in so long I've gotten rusty. This has gotta be easy, maybe someone will help with an example.

    I've got a form. It has three text input fields I'm interested in here: country, state, city.

    If the country is USA, I want city to be a required field. If it's not the US or USA, then it's not required. Any help?

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if (document.FormName.Country.value=="USA") {
    require state
    }

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm assuming you're using a country dropdown and a state dropdown -
    Edit: OOPS! I see you're using a text field for BOTH, VERY bad idea!


    You really need to use a <select> dropdown for both of them, because you can't depend (or validate) on what users might type in... like:

    Untied Status

    Misssisippii

    or whatever... (and believe me, I've seen worse than that!)

    Then, as suggested above, use something like:
    Code:
    var f = document.FormName;
    if(f.Country.options[f.Country.selectedIndex].value=="USA" && f.State.options[f.State.selectedIndex].value=="") { 
       alert('Yo buddy! You forgot to enter the State you live in!');
       return false;
    }
    Just another reason to use a dropdown - consider you might use state abbreviations to validate against (and/or save to a database). If someone were to instead enter:

    Nevada

    in a text field, and you only looked at the first two characters, your script would assume they entered "NE", which is actually Nebraska.

    I suggest using State/Country dropdowns because those are at least finite values which can easily be obtained. If you need a pretty up-to-date listing of states and/or countries and their respective values to use in a dropdown, let me know.
    Last edited by whammy; 01-10-2003 at 01:24 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yup. They are text fields. I didn't design it, just inherited it.

    I think I will convert them to drop-list boxes. Thanks for the suggestion.


  •  

    Posting Permissions

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