locked
$("form").validate(); does not seem to work RRS feed

  • Question

  • User-973886032 posted

    hi guys

    I am doing my client side validation with jquery and then using ajax post to submit to the server (WHICH WORKS FINE)

    PROBLEM IS

    I am using custom validation against whitespaces and and I want to set the model warnings, but apparently its not working.

    Here is my code

     $("#User_Chosen_Name").val($.trim($("#User_Chosen_Name").val()));
                        var inp = $("#User_Chosen_Name").val();
                        
                        if (inp.length <= 0) {
                            $('#User_Chosen_Name').css('border', '2px solid red');
                            $("#Button_Lists_Submit").prop('value', 'Try again');
                            $("form").validate();
                             alert('Please give your list a name - Thank you');
                            var F = $("form").validate();
                            F.focusInvalid();
                            return;
                        }

    I am trying out the 3 lines, but does not seem to work

     var F = $("form").validate();
    
    F.focusInvalid();
    
    $("form").validate();

    Tuesday, May 22, 2018 11:02 AM

All replies

  • User475983607 posted

    Can you explain what "Not Working" means?  Are you trying to stop users from submitting a space in the User_Chosen_Name input?

    https://api.jquery.com/jquery.trim/

    Or is something else "Not Working"?

    Tuesday, May 22, 2018 11:13 AM
  • User-973886032 posted

    no I am not trying to stop submitting. That I already used e.preventDefault()

    I am trying to focus on the form fields that are not properly validated e.g. in my code there is a textbox called User_Chosen_Name below

     <div class="form-group ">
                <label asp-for="User_Chosen_Name" class="control-label col-md-2">Give a Name</label>
                <input asp-for="User_Chosen_Name" class="col-2 form-control col-md-3 " />
                <span asp-validation-for="User_Chosen_Name" class="text-danger"></span>
            </div>
    

    When the user clicks submit, the jquery I posted above checks to see if their are whitespaces and if there is trim it and return an error to the label

    THIS WORKS if its empty, but if the trim function removes the whitepaces, it does not alert the user (or it does not show the label)

    Tuesday, May 22, 2018 11:19 AM
  • User-474980206 posted

    you code makes no sense. $("form).validate(), just return the validator, it doesn't validate. it also doesn't have a focusInvalid() method. custom validation should be done with a rule and method, not code like your sample code.

    see: the docs:

       https://jqueryvalidation.org/documentation/

    Tuesday, May 22, 2018 3:20 PM
  • User-973886032 posted

    you code makes no sense. $("form).validate(), just return the validator, it doesn't validate. it also doesn't have a focusInvalid() method. custom validation should be done with a rule and method, not code like your sample code.

    see: the docs:

       https://jqueryvalidation.org/documentation/

    thanks for the documentation,

    I had stripped my code down to a bare minimum to try to get some explanation on jquery validation. The documentation has been helpful

    Friday, May 25, 2018 8:36 PM