Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I made a mobile field as invalid by adding custom validity. But form is getting valid even after field is invalid.

var field = document.getElementById(textBoxId); field.setCustomValidity("Duplicate Mobile");



var $form = $(".modal-form");
$.validator.unobtrusive.parse($form);
if ($form.valid()) {

// $form is always valid
}


Can you suggest if i need to add any other check here?

Thanks,

What I have tried:

tried reportValidity, checkValidity. Didnt work.
Posted
Updated 27-Dec-20 1:19am

1 solution

Unfortunately, you can't style pseudo classes in JavaScript, since they're not real elements, they don't exist in the actual DOM.

With vanilla JavaScript you'd use the validation API.

In jQuery you can simply do this, http://jsfiddle.net/elclanrs/Kak6S/

if ( $input.is(':invalid') ) { ... }
Or alternatively, can I override the validation method used?

If you're using HTML5 validation, then stick to the markup. Otherwise, you can disable HTML5 validation with $form.attr('novalidate', 'novalidate') and use JS to validate your fields, and then adding valid or invalid classes where needed. I made a plugin that works like this to support non HTML5 browsers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900