[英]Validate email or mobile with regular expression
I have one form field. 我有一个表单字段。 In that field it's possible for user to enter EMAIL or MOBILE .
在该字段中,用户可以输入EMAIL或MOBILE 。 From a PHP page i got value.
从PHP页面中我获得了价值。 After that i want to check whether this is email id or mobile number.
之后,我要检查这是电子邮件ID还是手机号码。 suppose
email
means i want to email success,suppose mobile
means i want to show mobile success ,i think we have to write regular expression,but i don't know how to write regular expression for this problem? 假设
email
意味着我想通过电子邮件发送成功,假设mobile
意味着我想显示移动成功,我认为我们必须编写正则表达式,但是我不知道如何为这个问题编写正则表达式?
<form action="#" method="POST" id="forgotForm"> <div class="form-group has-feedback"> <input type="text" class="form-control" placeholder="Email OR Mobile" id="email" name="email" value="" aria-required="true" required="" data-msg-required="Please enter your email"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> </form> home.php <?php $email=$_POST['email'];//here value it will come like 9986128658 or admin@gmail.com ?>
You can check the input using preg_match
您可以使用
preg_match
检查输入
$email=$_POST['email'];
$emailPattern = '/^\w{2,}@\w{2,}\.\w{2,4}$/';
$mobilePattern ="/^[7-9][0-9]{9}$/";
if(preg_match($emailPattern, $email)){
echo "Email Success!";
} else if(preg_match($mobilePattern, $email)){
echo "Mobile Success!";
} else {
echo "Invalid entry";
}
Checks for the valid email 检查有效的电子邮件
aa@aa.aa
aa@aa.aa
/^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/
/^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/
{2, /^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/
{2, /^\\w{2,}@[\\w\\.]{2,}\\.\\w{2,4}$/
Checks for the valid mobile 检查有效的手机
$mobilePattern
to /^[0-9]{10}$/
$mobilePattern
更改为/^[0-9]{10}$/
You can check if the value is a valid email address. 您可以检查该值是否为有效的电子邮件地址。 If it is then you have an email, otherwise you can assume that it is a phone number:
如果是,则您有一封电子邮件,否则,您可以假定它是一个电话号码:
$email = null;
$phone = null;
// The "email" field has been submitted
if (isset($_POST["email"])) {
// If it is an email then set the email variable
if (filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)) {
$email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
}
// If it is a number then set the phone variable
else if (filter_input(INPUT_POST, "email", FILTER_VALIDATE_INT)) {
$phone = filter_input(INPUT_POST, "email", FILTER_SANITIZE_NUMBER_INT);
}
}
if ($email !== null) {
echo "Submitted email: {$email}";
}
if ($phone !== null) {
echo "Submitted phone number: {$phone}";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.