简体   繁体   中英

How do I validate multiple patterns?

I have a single form element that sends its value to a PHP script. This value needs to be validated and it there are 5 different sets that it can be grouped to,

x stands for digits 0-9

Set 1: ABxxTXxx

Set 2: ABxxTXAxxx

Set 3: ABCxxx

Set 4: Axxx

Set 5: xxx

Can this be done? Is there any library I could use?

Sorry for my above posting as i am still unable to add comment to a question.

hi innowqhy,

write 5 different regular expression matching 5 patterns that are given above function fieldsCheck(){

var re1=/AB\d\dTX\d\d$/;
var re2=/AB\d\dTXA\d\d$/;
var re3=/ABC\d{3}$/;
var re4=/A\d{3}$/;
var re5=/\d{3}$/;
var field= form.field.value;

if(re1.test(field) ||re2.test(field) ||re3.test(field) ||re4.test(field) ||re5.test(field) ){
alert("Test success");
return true;
}else{
alert("test failed");
}
}

You can use this code :

function validate($value){



    if(preg_match("/AB[\\w]{2}TX[\\w]{2}/", $value, $matches))
    {
        return 1;
    }
    elseif(preg_match("/AB[\\w]{2}TXA[\\w]{3}/", $value, $matches))
    {
        return 2;
    }
    elseif(preg_match("/ABC[\\w]{3}/", $value, $matches))
    {
        return 3;
    }
    elseif(preg_match("/A[\\w]{3}/", $value, $matches))
    {
        return 4;
    }
    elseif(preg_match("/[\\w]{3}/", $value, $matches))
    {
        return 5;
    }
    else
    {
        return 6;
    }

}


$str = "ABxxTXxx";
var_dump(validate($str));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM