PHP common form validation class

<?php
header('Content-Type: text/html; charset=utf-8');

class ValidatePost{
    // Verify that it is a combination of letters/numbers of the specified length
    function alphabetLeng($num1,$num2,$str)
    {
        return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))?true:false;
    }
    // Verify if it is a number of the specified length
    function numLeng($num1,$num2,$str){
        return (preg_match("/^[0-9]{".$num1.",".$num2."}$/i",$str))?true:false;
    }
    //Verify ID number
    function identityCard($str){
        return (preg_match('/(^([\d]{15}|[\d]{18}|[\d]{17}x)$)/',$str))?true:false;
    }
    //Verify email address
    function email($str){
        return (preg_match ('/ ^ [_ \. 0-9a-z -] + @ ([0-9a-z] [0-9a-z -] + \.) + [az] {2,4} $ / ', $ str))? true: false;
    }
    // Verify phone number
    function phoneNumber($str){
        return (preg_match("/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/",$str))?true:false;
    }
    //Verify the url address
    function url($str){
        return (preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/",$str))?true:false;
    }
}

$vp = new ValidatePost();
if($vp ->alphabetLeng(5, 10, 'onestopweb')){
    echo 'alphabetLeng true'.PHP_EOL;
}else{
    echo 'alphabetLeng false'.PHP_EOL;
}
if($vp ->numLeng(5, 10, '3248185961')){
    echo 'numLeng true'.PHP_EOL;
}else{
    echo 'numLeng false'.PHP_EOL;
}
if($vp ->identityCard('440883199010161234')){
    echo 'identityCard true'.PHP_EOL;
}else{
    echo 'identityCard false'.PHP_EOL;
}
if($vp ->email('[email protected]')){
    echo 'email true'.PHP_EOL;
}else{
    echo 'email false'.PHP_EOL;
}
if($vp ->phoneNumber('135-12345678')){
    echo 'phoneNumber' .PHP_EOL;
}else{
    echo 'phoneNumber false'.PHP_EOL;
}
if($vp ->url('http://onestopweb.iteye.com/demo.php?id=12345%name=chaoyi')){
    echo 'url true'.PHP_EOL;
}else{
    echo 'url false'.PHP_EOL;
}

 

Effect picture:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326344891&siteId=291194637