Home » Jquery » javascript – jQuery validation is not happening

javascript – jQuery validation is not happening

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have this script on my page:

 <script type='text/javascript'>
    $(document).ready(function() {
        $('#TotalHours').attr('name', 'TotalHours'); // jQuery validation requires field names to be set

        $.validator.addMethod("hours", function(value, element) {
            var totalHours = 0;
            $('select').each(function(i, sel) {
                var hours = $(sel).find(":selected").text();
                if (hours != "")
                    totalHours += parseFloat(hours);
            });
            var reportedTotalHours = parseFloat($("#TotalHours").text());
            var difference = abs(totalHours - reportedTotalHours);
            return difference >= 0.25; // discrepancy must be less than a quarter hour
        });

        $('form').bind('submit', function() {
            $("#TheForm").validate({
                rules: {
                    TotalHours: {
                        hours: true
                    }
                }
            });
        });
    });
</script>

I had it working the other day but now when the form is submitted it is not validated (if I set a breakpoint inside the function I’m binding to submit, it never gets hit)… what could I have broken?

edit: here is the handler for the submit button, in case it helps:

<script type='text/javascript'>
function __E374_onclick(){
    try{$("#TheForm").validate();
    if(!$("#TheForm").valid()){alert('The data you have entered is not valid. Please complete all required fields and correct any data entry errors.'); return;}
    var pm=$("#TheForm").serialize();$.post("/Action/Do/", pm, function(d){
        switch(true){
            case (null==d):dm("The server returned an unexpected response.");break;
            case (!d.Valid):case (!d.Successful):dm(d.Message);break;
            case (d.Successful):dm(d.Message);fu("EditAssessment");break;
            default:dm("An unexpected condition occurred.");break;}});
}catch(e){}}
</script>

and the submit button itself:

<input type='button' id="__E374" class="button" value="Save" onclick="__E374_onclick();"/>
How to&Answer: