HTML Validation for phone numbers - html

I am new to HTML,can any one please help me to validate a phone number only with '+' and numeric values and phone number shud not exceed 13 numbers,and to validate email with a '#' and a '.',I dont want to use javascript

In HTML5 you can use <input type='tel'> and <input type='email'>
You can also specify a specific pattern like <input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>
Something like pattern='^\+?\d{0,13}' Would give you an optional + and up to 13 digits

Email Validation - <input type="email" />, use the type as email.
Telephone number validation - <input type="tel" />. use the type as tel.
Reference:
https://www.sitepoint.com/client-side-form-validation-html5/
HTML5 phone number validation with pattern
HTML5 Email Validation

HTML and/or JavaScript will only validate email on client side, the more secure solution would be to validate email on the server side.
Having said that you can do basic validation in HTML 5 like
<form>
<input type="email" placeholder="Your email" required>
<input type="submit" value="Submit">
</form>

Related

Pattern attribute for Email validation is not working in html5

Using Html5 I've a view which contains an email input field.
So to validate the correct email address I am using pattern attribute for validation.
But that is not working correctly, the problem I am facing here is though I enter invalid email address as abc#gmail the validation is not working.
i tested the same regex pattern in fiddler, it is working fine there, but coming to my application it is not working correctly. Please help me out with this.
Here is my view:
<form>
<input type="email"
class="form-control"
data-val="true"
data-val-required="please enter an email address"
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+.[a-z]{2,4}$"
required>
<input type="submit" />
</form>
Regex should be
[a-z0-9._%+-]+#[a-z0-9.-]+.[a-z]{2,}$
instead of
[a-z0-9._%+-]+#[a-z0-9.-]+.[a-z]{2,4}$
So the html input would be:
<input type="email"
class="form-control"
data-val="true"
data-val-required="please enter an email address"
pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$"
required>
NOTE: difference in regex is {2,} and {2,4}

how to use ng-mask in angularjs

how to use ng-mask for validation of phone number having pattern (xxx)xxx-xxxx . I tried using "pattern" in the input html tag but not satisfied with the result
because each time when user enters the phone number, the user must type both '()' and '-',and this will make the user annoying. So suggest a way to overcome this.
<input type="tel" name="phoneno" maxlength=13 ng-model="phone.number" pattern="^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$" required/></div>
This should work:
<input type="text" ng-model="phone.number" name="phone" mask="(999) 999-9999" clean="true" ng-model="phone">

HTML5 email does not allow email with a period

The following email is not accepted with a period, is this a regular expression issue or some bug.
This works
abcdefg.jijklmn#abcdedghijklmnopgi.com
but the following doesn't (note uppercase letter in the email "A" and "J"), i think that is the problem
Abcdefg.Jijklmn#abcdedghijklmnopgi.com
Html element
<input type="email" class="form-control" name="email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[![\[][1]][1]a-z]{2,3}$" required="" value=""/>
Error below
It's because your regex doens't allow capitals.. simply allow capitals..
[a-zA-Z0-9._%+-]+#[a-z0-9.-]+\.[![\[][1]][1]a-z]{2,3}$
Regular expressions for emails can be very difficult. However, you don't actually need to use a pattern to validate emails as type="email" does that for you. Simply remove the pattern entirely and the clients browser will validate the email for you using their in-built regex
<input type="email" class="form-control" name="email" required="true" value=""/>
I can't actually seem to get email validation to work with the pattern attribute at all, as I think it's competing with type="email".

how to check if the inputted email has "#" on it

i have a text field for email which is set to required, i want it to show a text that you need to include "#" on your email. like how it displays "fill out this field" when you pressed the button without typing anything on the field. How can i do it? Thanks.
EXAMPLE:
<input type="text" name="email" required>
You can try to use the type in case you are using HTML5 as:
<input type="email" name="email" required>
If you are not using the HTML5 then you need to use some scripting language like Javascript to check the validity. For example in Javascript it would be like:
if(!document.getElementById("email").checkValidity())
In case of PHP it would be like
if (filter_var($email, FILTER_VALIDATE_EMAIL))
You can check it by using (since HTML5):
<input type="mail" name="email" required>
It will display a warning if format is not correct (tooltip, or red color around the field, depending on your browser.
Or you can check it on the server side, once you form is sent. For example with PHP, you can use filter_var
Use pattern attribute,
<input type="text" name="email" pattern=".*[#]+.*" />
<input type="email" name="email" required />
More details are at: http://www.w3schools.com/tags/att_input_pattern.asp

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>