Expectation :
I am working on login form in Angular. Users can login with Email/Phone. I need to validate the Email/Phone in single text-box.
Live Scenario :
Facebook implemented same type of functionality in login. we can login in Facebook via Email/Phone. But as per the research Facebook validates the user data by performing server side validations not the client side validations.
Tried so far :
function validate() {
var textBoxValue = document.getElementById('emailphone').value;
var emailPattern = /^[a-z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)?@[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/;
if(textBoxValue == '') {
alert('Please enter value');
return false;
} else if(isNaN(textBoxValue)) {
if(!(textBoxValue.match(emailPattern))) {
alert('Please enter valid email');
return false;
}
} else {
if(textBoxValue.length != 10) {
alert('Please enter valid phno');
return false;
}
}
}
<input type="text" name="emailphone" id="emailphone"/>
<input type="submit" value="Validate" onclick="validate()">
I am able to achieve this functionality using pure JavaScript but I want to implement it in Angular using ng-pattern
or if there is any work around in Angular.
I found this post on SO but not working as per my expectation.
Validation for email or phone number for same text field in angularjs
You can use angular form validation and ng-pattern directive for input use
ng-pattern="/^(?:\d{10}|\w+@\w+\.\w{2,3})$/"
Working Demo