开发者

Dynamic form field validation using jquery

Hi am generating input boxes dynamically base on database keys.

 <input type="text" id="101" name="101"> 
 <input type="text" id="302" name="302">
 <input type="text" id="5开发者_如何学运维01" name="501">
 <input type="text" id="601" name="601">

i want to make it mandatory fields + max length validation check using jquery


Check out http://docs.jquery.com/Plugins/validation

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 10em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
</style>

<script>
$(document).ready(function(){
    $("form[name=validation]").validate()
});
</script>

<form name="validation" action="" method="post">
    <p>
        <label for="101">101</label>
        <input type="text" id="101" name="101" class="required" maxlength="4">
    </p>
    <p>
        <label for="302">302</label>
        <input type="text" id="302" name="302" class="required" maxlength="4">
    </p>
    <p>
        <label for="501">501</label>
        <input type="text" id="501" name="501" class="required" maxlength="4">
    </p>
    <p>
        <label for="601">601</label>
        <input type="text" id="601" name="601" class="required" maxlength="4">
    </p>
    <p>
        <input class="submit" type="submit" value="Submit"/>
    </p>
</form>


 $('input[type=text]').each(function() { 
    if($(this).val() == '')
       alert('is Null');
    if($(this).val().length > 5)
       alert('max length execced');

 })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜