what is the easiest/best way to validate a form using js?
objective:
i want a user to enter information on a form, then i will use that info to generate different results.heres my code:
http://jsfiddle.net/5HUVm/<form id="form1" name="form1" method="post">
<table>
<tr>
<td>I am interested in:</td>
<td><input type="radio" name="choice" value="consume" id="consume"/> Cosuming and/or distributing OTC Markets data
</td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="choice" value="extranet" id="extranet"/> Providing connectivity to OTC Markets(Extranet)</td>
</tr>
<tr>
<td>Data products:</td>
<td><input type="checkbox" name="realtime" value="realtime" id="realTime">Real-time data</td>
<td><input type="checkbox" name="otcdata" value="otcdata" id="compData">OTC Company Data</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="eodprice" 开发者_运维百科value="eodprice" id="eod">End of Day Pricing Data</td>
<td><input type="checkbox" name="otccompliance" value="otccompliance" id="otcComp">OTC Compliance Data</td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="otcsecurity" value="otcsecurity" id="otcSec">OTC Security Data</td>
<td><input type="checkbox" name="otcqhistory" value="otchistory" id="otcHist">OTC Quote History</td>
</tr>
<tr>
<td>Data use:</td>
<td>
<select name="products" id="prod">
<option value="select">Select</option>
<option value="personal">Personal Use Only</option>
<option value="distEx">Distribute Externally Only</option>
<option value="distIn">Distribute Internally Only</option>
<option value="distBoth">Distribute Externally & Internally Only</option>
</select>
</td>
</tr>
<tr>
<td>Firm type:</td>
<td>
<select name="firm" id="firm">
<option value="select">Select</option>
<option value="broker">Broker-dealer</option>
<option value="oms">Order Management System</option>
<option value="finanInfo">Financial Information Service/Portal</option>
<option value="service">Service Bureau</option>
<option value="other">Other</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="reset" id="re">
<input type="button" id="sub" value="Submit" onclick="valid(this)">
</td>
</tr>
</table>
</form>
so as you can see there are checkboxes, radio buttons, drop downs.
what is the best way to validate what has been checked/selected?i was thinking:
lots of if statements then use a variable to store a special "numbers" for the different choices made then taht number will be used to generate the result pagefor example:
user chooses consuming, real time data, and end of day data that would be lets say choice 1 user clicks submit and the results page will show special links/articles.There are lots of validation scripts out there that will do exactly what your trying to do - don't reinvent the wheel .... go grab one and use it :
Prototype : http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype
JQuery : http://bassistance.de/jquery-plugins/jquery-plugin-validation/
YUI : http://www.yuiblog.com/blog/2009/12/03/yui-3-gallery-form-module/
The easiest way would be to use some form of plugin that has already been written but allows you to customise:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
精彩评论