问题
I am working on registration form for my project in AngularJs. Users can register with his/her Email/Phone. I need to validate that particular text-box.
I have validations for both, with different text fields, with ng-pattern. But how can I validate for both in one text field?
I have updated my code, here:- enter code here
http://plnkr.co/edit/mCVxHxl2DIvqOYtVapRN?p=preview
回答1:
Use /^([_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5}))|(\d+$)$/
Demo
回答2:
You can use a pipe '|' to OR two regular expressions together:
/<email-pattern>|<phone-pattern>/
so your final regex would be:
/^([_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5}))|\d+$/
回答3:
You can use angular form validation and ng-pattern directive for input use ng-pattern="/^(?:\d{10}|\w+@\w+\.\w{2,3})$/"
Working Demo
var app = angular.module("MY_APP", []);
app.controller("MyCtrl", function($scope) {
$scope.submitted = false;
$scope.submit = function(userData){
console.log(userData)
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MY_APP">
<div ng-controller="MyCtrl">
<form name="userForm" novalidate ng-submit="userForm.$valid && submit(userData)">
<div class="errorMsg" ng-show="submitted==true && userForm.$invalid">
<ul>
<li ng-show="submitted==true && userForm.emailphone.$error" class="errorMsg">
wrong format, It should be email or 10 digit mobile number
</li>
</ul>
</div>
<input type="text" name="emailphone" ng-model="userData.user" id="emailphone" required
ng-pattern="/^(?:\d{10}|\w+@\w+\.\w{2,3})$/" />
<button type="submit" ng-click="submitted = true">Submit</button>
</form>
</div>
</div>
回答4:
This code might work for you: Here is the ans of your question
<script type="text/javascript">
function validate(){
var phoneNo = document.getElementById('txtEmailormob');
var c=document.forms["myForm"]["email"].value;
var atpos=c.indexOf("@");
var dotpos=c.lastIndexOf(".");
var errorMsg = document.getElementById("errorMsg");
var successMsg = document.getElementById("successMsg");
if(phoneNo.value==""||phoneNo.value==null){
errorMsg.style.display="block";
document.getElementById("errorMsg").innerHTML = "Please enter your Mobile No or Email ";
return false;
}
if ((phoneNo.value.length < 10 || phoneNo.value.length > 10) && (atpos<1 || dotpos<atpos+2 || dotpos+2>=c.length)) {
errorMsg.style.display = "block";
successMsg.style.display = "none";
document.getElementById("errorMsg").innerHTML = " Mobile No. is not valid, Please Enter 10 Digit Mobile No or Please Enter valid Email. ";
return false;
}
successMsg.style.display = "block";
document.getElementById("successMsg").innerHTML = " Success ";
errorMsg.style.display = "none";
return true;
}
</script>
回答5:
Simple code
Complete Working Like Validate : amazon, Facebook, uber.etc Email or Phone Number Using Single Input...
Try this its working ..............
<script>
$(document).ready(function () {
$("#cuntryCode").hide();
$("#useridInput").on('input', function () {
var len = $("#useridInput").val().length;
if (len >= 2) {
var VAL = this.value;
var intRegex = /^[1-9][0-9]*([.][0-9]{2}|)$/;
if (!intRegex.test(VAL)) {
$('#error-caption').html('Invalid email. Please check spelling.');
$('#error-caption').css('color', 'red');
$("#cuntryCode").hide();
} else {
if(len < 10){
$('#error-caption').html('Invalid mobile number. Please try again.');
$('#error-caption').css('color', 'red');
$("#cuntryCode").show();
}else{
$('#error-caption').html('Invalid mobile number. length must be 10 digit.');
$('#error-caption').css('color', 'red');
}
}
}else{
$("#cuntryCode").hide();
$('#error-caption').html('');
}
});
});
</script>
<form class="push--top-small forward" method="POST" data-reactid="15">
<h4 id="input-title" data-reactid="16">Welcome back
</h4>
<label class="label" id="input-label" for="useridInput" data-reactid="17">Sign in with your email address or mobile number.
</label>
<div style="margin-bottom:24px;" data-reactid="18">
<div >
<div id="cuntryCode" class="_style_4kEO6r" style="float: left; height: 44px; line-height: 44px;">
<div tabindex="0" > +241
</div>
</div>
<div style="display:flex;">
<input id="useridInput" autocorrect="off" autocapitalize="off" name="textInputValue" class="text-input" placeholder="Email or mobile number" aria-required="true" aria-invalid="false" aria-describedby="error-caption input-title">
</div>
</div>
<div id="error-caption">
</div>
</div>
<button class="btn btn--arrow btn--full" data-reactid="24">
<span class="push-small--right" data-reactid="25">Next
</span>
</button>
</form>
来源:https://stackoverflow.com/questions/32776182/validation-for-email-or-phone-number-for-same-text-field-in-angularjs