SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru hifigrafix's Avatar
    Join Date
    Oct 2005
    Location
    Nashville, TN
    Posts
    628
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem With Select Validation

    For some reason I can't enforce selecting from the "seminar" field

    **TAKEN FROM JAVASCRIPT KIT

    HTML Code:
    /*
    Check required form elements script-
    By JavaScript Kit (http://javascriptkit.com)
    Over 200+ free scripts here!
    */
    
    function checkrequired(which){
    var pass=true
    if (document.images){
    for (i=0;i<which.length;i++){
    var tempobj=which.elements[i]
    if (tempobj.name.substring(0,8)=="required"){
    if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
    pass=false
    break
    }
    }
    }
    }
    if (!pass){
    alert("One or more of the required elements are not completed. Please complete them, then submit again!")
    return false
    }
    else
    return true
    }
    HTML Code:
    <form name="seminarRegister" method="post" action="***" onSubmit="return checkrequired(this)">
    
    <table>
    	<tr>
    		<td colspan="2" align="center"><strong>Seminar Registration Form</strong></td>
    	</tr>
    	<tr>
    		<td>First Name:</td><td><input type="text" name="requiredfirstName" /> *</td>
    	</tr>
    
    	<tr>
    		<td>Last Name:</td><td><input type="text" name="requiredlastName" />
    		*</td>
    	</tr>
    	<tr>
    		<td>Title:</td><td><input type="text" name="title" /></td>
    	</tr>
    
    	<tr>
    		<td>Company / Facility Name:</td><td><input type="text" name="company" /></td>
    	</tr>
    	<tr>
    		<td>Mailing Address:</td><td><input type="text" name="requiredaddress" />
    		*</td>
    	</tr>
    
    	<tr>
    		<td>City:</td><td><input type="text" name="requiredcity" />
    		*</td>
    	</tr>
    	<tr>
    		<td>State:</td><td><select name="requiredstate">
    		<option value=""></option>
    
    <option value="AL">AL</option><option value="AK">AK</option><option value="AZ">AZ</option><option value="AR">AR</option><option value="CA">CA</option><option value="CO">CO</option><option value="CT">CT</option><option value="DE">DE</option><option value="DC">DC</option><option value="FL">FL</option><option value="GA">GA</option><option value="HI">HI</option><option value="ID">ID</option><option value="IL">IL</option><option value="IN">IN</option><option value="IA">IA</option><option value="KS">KS</option><option value="KY">KY</option><option value="LA">LA</option><option value="ME">ME</option><option value="MD">MD</option><option value="MA">MA</option><option value="MI">MI</option><option value="MN">MN</option><option value="MS">MS</option><option value="MO">MO</option><option value="MT">MT</option><option value="NE">NE</option><option value="NV">NV</option><option value="NH">NH</option><option value="NJ">NJ</option><option value="NM">NM</option><option value="NY">NY</option><option value="NC">NC</option><option value="ND">ND</option><option value="OH">OH</option><option value="OK">OK</option><option value="OR">OR</option><option value="PA">PA</option><option value="RI">RI</option><option value="SC">SC</option><option value="SD">SD</option><option value="TN">TN</option><option value="TX">TX</option><option value="UT">UT</option><option value="VT">VT</option><option value="VA">VA</option><option value="WA">WA</option><option value="WV">WV</option><option value="WI">WI</option><option value="WY">WY</option></select>
    
    		*</td>
    	</tr>
    	<tr>
    		<td>Zip:</td><td><input type="text" name="requiredzip" size="9" />
    		*</td>
    	</tr>
    	<tr>
    
    		<td>Email:</td><td><input id="email" type="text" name="requiredemail" />
    		*  </td>
    	</tr>
    	<tr>
    		<td>Phone:</td><td><input type="text" name="requiredphone" />
    		* (xxx-xxx-xxxx)</td>
    	</tr>
    
    	<tr>
    		<td>Fax:</td><td><input type="text" name="fax" /></td>
    	</tr>
    	
    	<tr>
    		<td>Select the specific seminar:</td><td><select name="requiredseminar" id="seminar">
    <option value="">-Choose One-</option>
    <!-- EXAMPLES -->
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select></td>
    	</tr>
    	<tr>
    
    		<td>Indicate Payment Method:</td><td><input type="checkbox" name="payment[]" value="Check" /> Check <input type="checkbox" value="Credit Card" name="payment[]" /> Credit Card <input type="checkbox" name="payment[]" value="Purchase Order" /> Purchase Order <input  type="checkbox" name="payment[]" value="" /> Other</td>
    	</tr>
    	<tr>
    		<td>If "Other" please specify:</td><td><input type="text" name="payment[]" value="" /></td>
    
    	</tr>
    	<tr>
    		<td>Payment Details:</td><td><input type="text" name="payment_details" value="" /></td>
    	</tr>
    	<tr>
    		<td>Comments:</td><td><textarea name="comments"></textarea></td>
    	</tr>
    
    	<tr>
    		<td>&nbsp;</td><td><input type="checkbox" name="newsletter" checked="checked" value="1" /> Yes, please sign me up for your <strong>free</strong> newsletter.</td>
    	</tr>
    	<tr>
    		<td>&nbsp;</td><td><input type="submit" name="register" value="Register" /></td>
    	</tr>
    
    </table>
    I appreciate any help.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use

    Code:
    if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.value==-1)){
    instead of
    tempobj.selectedIndex==-1

    Also assign the first option value of the select box to "-1"

    Code:
    <select name="requiredstate">
    <option value="-1">Select State</option>
    <option value="AL">AL</option>
    <option value="AK">AK</option>
    .....
    ..
    </select>


Bookmarks

Posting Permissions

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