Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery validation only works for the first element in the form submitted. [intend to validate all]

The JQuery validation only works for the first element in the form submitted, Validation won't check for the 2nd one. Need to find out what is missing. Swap the input for text1 and date2, validation still works for the first one.

<!DOCTYPE HTML>
<html lang="en-US">
<head><title>
</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.1.min.js'></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.5.5/jquery.validate.min.js"></script> 
<script type="text/javascript" src="http://jquery-multifile-plugin.googlecode.com/svn-history/r16/trunk/jquery.MetaData.js"></script> 
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css"/>

<script type="text/javascript">
    $(document).ready(function () {
        $("#form1").validate();

        $(".date").datepicker({
            changeYear: true,
            dateFormat: 'dd/mm/yy',
            minDate: new Date('1990/01/01'),
            maxDate: '30Y'
        });
    });
    function test() {
        alert(document.getElementById("datetext").getAttribute("must"));
    }        
</script>
</head>
<body>
        <form method="post" action="WebForm1.aspx" id="form1">
           Text:<font color="red">* </font><input id="Text1" class="lvl {required:true,messages:{required:'blank not allowed.'}}"  /><br />
            Select Date2:<font color="red">* </font><input id="date2"  class="date {required:true,messages:{required:'Required:Need to enter 2 date.'}}" readonly="true" /><br />
            <input type="submit" value="Submit"/>
        </form>
</body>
</html>
like image 240
T.J. DAY Avatar asked Jul 18 '12 20:07

T.J. DAY


1 Answers

Be sure to include a name attribute to your inputs. I believe it uses the name attribute to build its list of validation fields and since you haven't specified a name attribute, jquery validation thinks the second field is the same name as the first.

For example, <input id="Text1" name="Text1" />

like image 188
Mark Avatar answered Nov 15 '22 19:11

Mark