Can I mark a field invalid from javascript?

JavascriptHtmlCssValidation

Javascript Problem Overview


From reading this post I have found that there are some pseudo classes for 'valid' and 'invalid' input values introduced to HTML5.

Is there a way I can mark an input field as invalid/valid from javascript? Or alternatively, can I override the validation method used?

Javascript Solutions


Solution 1 - Javascript

You could use the customValidity function for this purpose.

If you add a customValidity message to the field, it becomes invalid. When you set the message as an empty string, it becomes valid again (unless it is invalid because of other reasons).

field.setCustomValidity("Invalid field."); will make the field invalid.

field.setCustomValidity(""); will make the field valid unless it fails an HTML5 constraint.

Solution 2 - Javascript

Edit: Someone clarified that you're looking for "valid" "invalid" attributes for DOM.

I would add attributes to each tag using dom_object.setAttribute("isvalid", "true"). You could also have a central validation function which updates these attributes each time (and use dom_object.getAttribute("isvalid") each time).

You could run this function each time an element loses focus, or whenever you want.

Not exactly elegant, but unfortunately there's no "pseudo" support with javascript and HTML5 now.


If I understand your question, you can do validation with Javascript. However, be warned that it is very easy to circumvent client side validation, especially javascript validation. You should never trust client data and always do checking on the server side.

For example, I could easily find element IDs by inspecting the source code, then do document.getElementById('some_id').setAttribute('max', new_number) to change the max value (this was one of the entries from your link).

There are various ways to do it, so I'll try to give you the general idiom.

You can grab the value by doing document.getElementById('form_element_id').value (make sure you give the form a name which is sent to the server, and an id which is used by javascript). For textareas, you can use .innerHTML.

Then you have the value in a variable, there are various ways to check it.

For example, you could do if (parseInt(my_value) < 0) //error. You could also use regular expressions, I won't explain it all but you could start here http://www.w3schools.com/jsref/jsref_obj_regexp.asp . I know w3schools isn't the best source but I find it an okay place to start.

Now for the validation part: add onsubmit="return validateForm() to your form tag where validateForm() is the function which does all the checking. And the function just returns true if valid and false otherwise. This overrides the default validation function (which by default does nothing).

So in the above example, //error would be replaced by return false. You can do other things too; such as alert the error then return false. You could also use javascript to highlight the invalid fields (not sure if this is what you mean by "mark an input field as invalid/valid from javascript")

Of course, if you don't want to check all the fields you only have to return true if the certain ones pass. Again, you shouldn't rely on this, but if you're only out to deter average people then it's an easy solution.

Solution 3 - Javascript

> Is there a way I can mark an input field as invalid/valid from > javascript?

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSvishView Question on Stackoverflow
Solution 1 - JavascriptdajavaxView Answer on Stackoverflow
Solution 2 - JavascriptRaekyeView Answer on Stackoverflow
Solution 3 - JavascriptelclanrsView Answer on Stackoverflow