php开发之常用验证方法

//邮箱验证

function isEmail($email) {

if (!$email) {

return false;

}

return preg_match(‘/^[_\.0-9a-z-][email protected]([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/‘, $email);

}

// 手机号验证

function isMobile($mobile) {

if (!$mobile) {

return false;

}

return preg_match(‘/^((\(d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{9}$/‘, $mobile);

}

// 邮编验证

function isPostalCode($postalCode) {

if (!$postalCode) {

return false;

}

return preg_match("/^[1-9]\d{5}$/", $postalCode);

}

// ip验证

function isIPAddress($IPAddress) {

if (!$IPAddress) {

return false;

}

return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" .

"(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $IPAddress);

}

// 身份证验证

function isIDCard($IDCard) {

if (!$IDCard) {

return false;

}

return preg_match(‘/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/‘, $IDCard);

}

/**

* 检查中文

* @param string $str 标签字符串

*/

function isCn($str){

if(preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str)) {

return true;

}

return false;

}

/**

* 检查数字

* @param string $str 标签字符串

*/

function isNumber($str){

if(preg_match(‘/^\d+$/‘, $str)) {

return true;

}

return false;

}

/**

* 检查是否每位相同

* @param string $str 标签字符串

*/

function isNumSame($str){

if(preg_match(‘/^(\w)\1+$/‘, $str)) {

return true;

}

return false;

}

/**

* 检查是否为空

* @param string $str 标签字符串

*/

function isEmpty($str){

//$str = trim($str);

if(preg_match(‘/^\s*$/‘, $str)) {

return true;

}

return false;

}

/**

* 检测是否为合法url

*/

function isUrl($url){

if(strpos(‘kkk‘ . $url, ‘http‘)){

return true;

}

return false;

}

// 检测一组字符是否有可能组成手机号码

function willMobile($mobile) {

if (!$mobile) {

return false;

}

return preg_match(‘/^((\(d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{0,9}$/‘, $mobile);

}

function isPhoneNumber($phone) {

if (!$phone) {

return false;

}

echo($phone);

return preg_match(‘/^((0\d{3}[\-])?\d{7}|(0\d{2}[\-])?\d{8})?$/‘, $phone);

}

function isAreaCode($code){

if (!$code) {

return false;

}

return preg_match(‘/^(0\d{3})|(0\d{2})$/‘, $code);

}

/**

* 参数验证

* @param $para 数据

* @param $standard 参数要求

* @return boolen

*/

public static function verifyParams($para, $standard)

{

if ($para === false || empty($para)) {

return false;

}

foreach ($standard[‘REQUIRED‘] as $k => $v) {

if (!array_key_exists($k, $para)) {

return false;

}

if(empty($para[$k])){

return false;

}

if (‘string‘ == $v) {

if (false === is_string($para[$k])) {

return false;

}

} else if (‘int‘ == $v) {

if ((string)((int)($para[$k])) != $para[$k]) {

return false;

}

} else{

return false;

}

}

foreach ($standard[‘OPTIONAL‘] as $k => $v) {

if (!array_key_exists($k, $para)) {

continue;

}

if (‘string‘ == $v) {

if (!empty($para[$k]) && false === is_string($para[$k])) {

return false;

}

} else if (‘int‘ == $v) {

if (!empty($para[$k]) && (string)((int)($para[$k])) != $para[$k]) {

return false;

}

} else {

return false;

}

}

return true;

}

原文地址:https://www.cnblogs.com/xingxia/p/php_validate.html

时间: 2024-07-31 17:17:59

php开发之常用验证方法的相关文章

前端开发之常用验证方法

在日常开发中,经常需要对提交的数据进行验证处理,总结一下常用的验证方法. 1)邮箱验证 function checkEmail(email) { var reg = /^([a-zA-Z0-9_-])[email protected]([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/; return reg.test(email); //检测是否匹配 } 2)电话验证 // 判断是否为手机号 function isPoneAvailable(pone)

iOS开发UIPickerView常用属性方法

// //  ViewController.m //  UIPickerViewAll #import "ViewController.h" @interface ViewController () @end @implementation ViewController /* UIPickView控件常用的方法和属性: (1)  - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 返回Picke

【java】开发中常用字符串方法

java字符串的功能可以说非常强大, 它的每一种方法也都很有用. java字符串中常用的有两种字符串类, 分别是String类和StringBuffer类. Sting类 String类的对象是不可变的. 创建String String() String(String str) String(char value[]) //用字符数组生成一个串对象 String(char value[], int offset, int count) //用字符数组value的offset位开始的count个字

rails常用验证方法 (转)

validates_presence_of       :login,  :message => "用户名不能为空!" validates_length_of           :login, :minimum => 4,   :message => "用户名长度须为4到20位字母或数字!"   validates_uniqueness_of   :login,:case_sensitive => false, :message =>

JS常用验证方法

1.验证必须为数字(可有小数点) if(isNaN(value))execCommand('undo') 说明:1.isNaN()方法用于验证value值是否为非法数字,返回值true或者false. 2.execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令,该例子命令undo意味"撤销".因此当isNaN()返回true即"撤销"

常用验证方法

1.手机号验证: //手机号验证function checkMobile(sMobile){ //var sMobile = $("#phone").val(); if(!(/^1[34578]\d{9}$/.test(sMobile))){ alert("手机号输入有误,请重新输入"); return false; } return true; }; 2.身份证验证: //身份证验证 function checkIdcar(Idcar){ //var Idcar

iOS开发UItextview常用属性方法

// //  ViewController.m //  TextViewAll #import "ViewController.h" @interface ViewController ()<UITextViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowC

Android开发中常用到方法总结

1.判断服务是否在运行中 public static boolean isServiceRunning(Context context, String serviceName) {  boolean isRunning = false;  ActivityManager activityManager = (ActivityManager) context    .getSystemService(Context.ACTIVITY_SERVICE);  List<ActivityManager.

android开发通知常用设置方法

简单记录 通知上的设置方法,没有示例 //进度 通知 notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(context); builder.setContentTitle("新版本")//显示的标题 .setContentText("正在下载...