faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 6 people (33%) answered Yes
Recently 2 of 6 people (33%) answered Yes

Entry

I have 2 textfields in form: country & state, if country is USA, state should not be blank

Apr 6th, 2008 19:44
ha mo, Alan Frey, d m,


First, you may want to consider using drop-down boxes instead of text-
boxes in this situation.A drop-down box will allow you to dictate all 
available answers to the user, which will make form validation a good 
bit easier. 
Here is the code for use with drop-down boxes, I did not include all 
countries, but I did get all of the states (I think!)
Just copy the following code into an html editor and see how it works.
ATF
<html>
<head>
	<script language="JavaScript">
		function handleCountry(){
			if (frmRegistration.selCountry.value == "USA"){
				frmRegistration.selState.disabled = 
false;
			}
		}
		function validate(){
			var strCountry = 
frmRegistration.selCountry.value;
			switch(strCountry){
				case "NULL":
					alert("You must select a valid 
Country");
				break;
				case "USA":
					if 
(frmRegistration.selState.value == "NULL"){
						alert("You must select 
a valid State when choosing USA as your country");
					}
					else{
						submitForm();
code here
					}
				break;
			        default: 
                                             submitForm();
			}		
		}
             function submitForm(){
                     //place form submit code here
             }
	</script>
</head>
<body>
	<form name="frmRegistration">
		<select name="selCountry" id="selCountry" 
onchange="handleCountry()">
			<Option value="NULL">Choose One </option>
			<OPTION VALUE="USA">United States</OPTION>
            <OPTION VALUE="CAN">Canada</OPTION>
        	<OPTION VALUE="GBR">United Kingdom</OPTION>
        	<OPTION VALUE="FRA">France</OPTION>
        	<OPTION VALUE="DEU">Germany</OPTION>
        	<OPTION VALUE="JPN">Japan</OPTION>
        </select>
		<select name="selState" id="selState" disabled>
			<OPTION VALUE="NULL">Choose One</OPTION>
					<OPTION VALUE="AL">Alabama 
</OPTION>
					<OPTION 
VALUE="AK">Alaska</OPTION>
					<OPTION 
VALUE="AZ">Arizona</OPTION>
					<OPTION 
VALUE="AR">Arkansas</OPTION>
					<OPTION 
VALUE="AK">California</OPTION>
					<OPTION 
VALUE="CO">Colorado</OPTION>
					<OPTION 
VALUE="CT">Connecticut</OPTION>
					<OPTION 
VALUE="DE">Delaware</OPTION>
					<OPTION VALUE="DC">District of 
Columbia</OPTION>
					<OPTION 
VALUE="FL">Florida</OPTION>
					<OPTION 
VALUE="GA">Georgia</OPTION>
					<OPTION VALUE="GU">Guam</OPTION>
					<OPTION 
VALUE="HI">Hawaii</OPTION>
					<OPTION 
VALUE="ID">Idaho</OPTION>
					<OPTION 
VALUE="IL">Illinois</OPTION>
					<OPTION 
VALUE="IN">Indiana</OPTION>
					<OPTION VALUE="IA">Iowa</OPTION>
					<OPTION 
VALUE="KS">Kansas</OPTION>
					<OPTION 
VALUE="KY">Kentucky</OPTION>
					<OPTION 
VALUE="LA">Louisiana</OPTION>
					<OPTION 
VALUE="ME">Maine</OPTION>
					<OPTION 
VALUE="MD">Maryland</OPTION>
					<OPTION 
VALUE="MA">Massachusetts</OPTION>
					<OPTION 
VALUE="MI">Michigan</OPTION>
					<OPTION 
VALUE="MN">Minnesota</OPTION>
					<OPTION 
VALUE="MS">Mississippi</OPTION>
					<OPTION 
VALUE="MO">Missouri</OPTION>
					<OPTION 
VALUE="MT">Montana</OPTION>
					<OPTION 
VALUE="NE">Nebraska</OPTION>
					<OPTION 
VALUE="NV">Nevada</OPTION>
					<OPTION VALUE="NH">New 
Hampshire</OPTION>
					<OPTION VALUE="NJ">New 
Jersey</OPTION>
					<OPTION VALUE="NM">New 
Mexico</OPTION>
					<OPTION VALUE="NY">New 
York</OPTION>
					<OPTION VALUE="NC">North 
Carolina</OPTION>
					<OPTION VALUE="ND">North 
Dakota</OPTION>
					<OPTION VALUE="OH">Ohio</OPTION>
					<OPTION 
VALUE="OK">Oklahoma</OPTION>
					<OPTION 
VALUE="OR">Oregon</OPTION>
					<OPTION 
VALUE="PA">Pennsylvania</OPTION>
					<OPTION VALUE="PR">Puerto 
Rico</OPTION>
					<OPTION VALUE="RI">Rhode 
Island</OPTION>
					<OPTION VALUE="SC">South 
Carolina</OPTION>
					<OPTION VALUE="SD">South 
Dakota</OPTION>
					<OPTION 
VALUE="TN">Tennessee</OPTION>
					<OPTION 
VALUE="TX">Texas</OPTION>
					<OPTION VALUE="UT">Utah</OPTION>
					<OPTION 
VALUE="VT">Vermont</OPTION>
					<OPTION VALUE="VI">Virgin 
Islands</OPTION>
					<OPTION 
VALUE="VA">Virginia</OPTION>
					<OPTION 
VALUE="WA">Washington</OPTION>
					<OPTION VALUE="WV">West 
Virginia</OPTION>
					<OPTION 
VALUE="WI">Wisconsin</OPTION>
					<OPTION 
VALUE="WY">Wyoming</OPTION>
			</select>
			<input type="button" value="Submit" 
onclick="validate()"/>
	</form>
</body>
</html>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com