Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a minlength validation attribute in HTML5?

It seems the minlength attribute for an <input> field doesn't work.

Is there any other attribute in HTML5 with the help of which I can set the minimal length of a value for fields?

like image 418
emilan Avatar asked Apr 23 '12 13:04

emilan


People also ask

Does HTML have Minlength?

The minlength attribute defines the minimum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no minlength is specified, or an invalid value is specified, the input has no minimum length.

Which HTML5 attribute is used for data validation?

The pattern attribute of the <input> element allows you to add basic data validation without resorting to JavaScript. It works by matching the input value against a regular expression.

Does HTML5 have form validation?

Using built-in form validationOne of the most significant features of HTML5 form controls is the ability to validate most user data without relying on JavaScript. This is done by using validation attributes on form elements.

Does HTML5 support client side validation?

Client side validation occurs using HTML5 attributes and client side JavaScript. You may have noticed that in some forms, as soon as you enter an invalid email address, the form gives an error "Please enter a valid email". This immediate type of validation is usually done via client side JavaScript.


1 Answers

You can use the pattern attribute. The required attribute is also needed, otherwise an input field with an empty value will be excluded from constraint validation.

<input pattern=".{3,}"   required title="3 characters minimum"> <input pattern=".{5,10}" required title="5 to 10 characters"> 

If you want to create the option to use the pattern for "empty, or minimum length", you could do the following:

<input pattern=".{0}|.{5,10}" required title="Either 0 OR (5 to 10 chars)"> <input pattern=".{0}|.{8,}"   required title="Either 0 OR (8 chars minimum)"> 
like image 178
user123444555621 Avatar answered Nov 02 '22 14:11

user123444555621