使用正则表达式验证电子邮件或手机 [英] Validate email or mobile with regular expression
本文介绍了使用正则表达式验证电子邮件或手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单字段.在该字段中,用户可以输入 EMAIL 或 MOBILE .从PHP页面中我获得了价值.之后,我要检查这是电子邮件ID还是手机号码.假设email
表示我想通过电子邮件发送成功,假设mobile
表示我想显示移动成功,我认为我们必须编写正则表达式,但是我不知道如何为该问题编写正则表达式?
I have one form field. In that field it's possible for user to enter EMAIL or MOBILE. From a PHP page i got value. After that i want to check whether this is email id or mobile number. 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?
<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
?>
推荐答案
您可以使用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
- TLD至少应包含2个字符,最多4个字符
- 要包含诸如co.in之类的域,请使用-
/^\w{2,}@[\w\.]{2,}\.\w{2,4}$/
- Email should have atleast two words length say
aa@aa.aa
- TLD should have atleast 2 characters and maximum of 4 characters
- To include domains like co.in, use -
/^\w{2,}@[\w\.]{2,}\.\w{2,4}$/
检查有效的手机
- 移动设备应具有10个字符的长度,并且应以7或8或9开头,要删除该限制,请将
$mobilePattern
更改为/^[0-9]{10}$/
- Mobile should have 10 characters length and should start either with 7 or 8 or 9, to remove that restriction, change the
$mobilePattern
to/^[0-9]{10}$/
这篇关于使用正则表达式验证电子邮件或手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文