Match a string but not a blank space using HTML5 input pattern - html

I'm using the HTML5 pattern attribute in an <input> to return a match for a specific string: 'felfogtam'
My current markup is:
<input type="text" class="input input-small" pattern="felfogtam">
It does work, but the input is still valid if the field is empty. How can I change the regular expression to not allow a blank field?

Add the required attribute
<input type="text" class="input input-small" pattern="felfogtam" required>
http://jsfiddle.net/bJU2D/

Related

input tag pattern accepting blank values

I have the following input field:
<input
type="email"
id="emailAddr"
name="emailAddr"
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$"
placeholder="name#domain.com"
/>
Surprisingly, it allows me to enter the empty email field, whereas if I type something and submit it, it shows an error if the email is not valid.
As described in the specification, constraint validation for the pattern attribute is not performed if the input value is an empty string.
You need to add a required attribute to your element:
<input
type="email"
id="emailAddr"
name="emailAddr"
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$"
placeholder="name#domain.com"
required
/>
Input already allows for email validation, so no need to add in a pattern, just use:
<input type="email" id="email" name="email">
HTML5 Email Validation

restrict special characters into input box

Am new on Angular.Need to restrict special characters into input box with angularjs
HTML code:
<input type="text" class="form-control" id="cntry" ng-model="address.countryCode">
Allow only alphabets or digits
You can try this solution by adding pattern:
<input type="text" class="form-control" id="cntry"
ng-model="address.countryCode" pattern="^[a-zA-Z0-9]*$}">
Define a regex in your controller
$scope.regex = /^[^`~!##$%\^&*()_+={}|[\]\\:';"<>?,./1-9]*$/;
And in your html use a ng-pattern directives and pass the above regex as pattern.
<input type="text" class="form-control" id="cntry"
ng-model="address.countryCode" ng-pattern="regex">
For more info visit Restrict Special Characters in HTML & AngularJs

How do i make a specific pattern for my html input

I'm trying to make a custom pattern for my html input but have no idea how to do so!
The pattern I want: ABC-A0-01
So basically the first part has uppercase alphabets only, second part has uppercase with numeric values, and the last part is numeric only and is separated by a '-'
<input type="text" class="form-control txtsize" id="equi" placeholder="Insert equipment name e.g ABC-A0-12" data-ng-model="equipmentToAdd">
You could use the pattern attribute with a RegEx such as this: pattern="[A-Z]{3}[-][A-Z]{1}[0-9]{1}[-][0-9]{2}".
Try inputting an invalid value, and hit submit. The browser will give an error with the message from title property.
<form>
<input type="text" class="form-control txtsize" id="equi" placeholder="Insert equipment name e.g ABC-A0-12" data-ng-model="equipmentToAdd" pattern="[A-Z]{3}[-][A-Z]{1}[0-9]{1}[-][0-9]{2}" title="Insert equipment name e.g ABC-A0-12">
<button type="submit">Submit</button>
</form>

HTML5 novalidate only for some inputs

I've got really simple question - is there any way to disable HTML5 validation only for some chosen inputs (instead of setting "novalidate" for whole form)?
I mean something like <input type='number' requirednovalidate>. But this doesn't work.
You may ask why I need type="number" or "required" then? Well, I need it there because my framework uses it for its own validation.
EDIT
It is about one special input - birth number. I need it to be of type number (because of mobile devices) but its value is mostly used with "/" (e.g. 860518/8757) which is not valid character for type number. So I need user to fill it without slash (8605188757). The problem is when there is invalid value filled in html5 input (e.g. "fsda" in number type), it seems like it is empty, with no value.
So when user fill the value in wrong format (860518/8757), html validation is disabled so the JS validation runs, it is validated like empty field. So the error message is like "Please fill the field birth number" (which is really confusing) instead somthing like "Sorry, wrong format".
My solution was to enable html5 validation for this field (so the default browser message is displayed when there is wrong format filled) but disable it for other fields so that they would be validated only with my JS validation.
You cannot disable HTML5 validation for a chosen input(s).
If you want to remove validation on the entire form you can use formnovalidate in your input element.
For example,
<input type="submit" value="Save" class="button primary large" formnovalidate/>
Note you can use formnovalidate with <input type=submit>, <inut type=image> or <button> -source
For more info go here or here.
novalidate attribute is only for form tag, it can't be applied on form controls.
You can remove the required attribute in js, after your framework validates:
$('[Selector]').removeAttr('required');​​​​​
Now the selected field will not be validated.
Inputs will be validate when:
have attr required or prop required=true
aren't empty; don't have to have attr required or prop required=true
and have no attr disabled or prop disabled=true
If you want to validate data in a specific way, use pattern attr.
JSFiddle
(function() {
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
});
})();
<form>
1. <input type='text'><br>
2. <input type='text' required><br>
3. <input type='text' required disabled><br>
4. <input type='text' value="" pattern="\d*"><br>
5. <input type='text' value="" pattern="\d*" required><br>
6. <input type='text' value="" pattern="\d+"><br>
7. <input type='text' value="" pattern="\d+" required><br>
8. <input type='text' value="test" pattern="\d+" required disabled><br>
<button>check field validity</button>
</form>

is it autofocus="autofocus" or autofocus?

I seem to recall most (maybe all) attributes in previously versions of HTML (before HTML5) required attributes to have values, like readonly="readonly".
Is this true for HTML5 and the autofocus attribute?
In HTML, you use boolean attributes with or without values as you like.
A boolean, for W3C, like autofocus can be written like that autofocus or autofocus="autofocus" or also autofocus="".
If you don't want autofocus just don't write it.
I think you are confused because XHTML requires values for all attributes: attributes="values".
Here is some information about boolean attribute use in HTML:
http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute
Quoting the HTML5 spec and expanding a bit on Pekka:
http://www.w3.org/TR/html5/forms.html#autofocusing-a-form-control:-the-autofocus-attribute :
The autofocus attribute is a boolean attribute.
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :
The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
Conclusion:
The following are valid, equivalent and true:
<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />
The following are invalid:
<input type="text" autofocus="0" />
<input type="text" autofocus="1" />
<input type="text" autofocus="false" />
<input type="text" autofocus="true" />
The absence of the attribute is the only valid syntax for false:
<input type="text"/>
Recommendation
If you care about writing valid XHTML, use autofocus="autofocus", since <input autofocus> is invalid and other alternatives are less readable. Else, just use <input autofocus> as it is shorter.
No, it's enough to specify the attribute itself. It was that way also in HTML 4.
A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.
If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.
Example:
<label><input type=checkbox checked name=cheese disabled> Cheese</label>