Jorge Aguilar Jorge Aguilar - 6 days ago 5
PHP Question

HTML - Pattern to validate nomenclature

I'm trying to use the "pattern" attribute for HTML5 but I do not have much experience in the regex.

The nomenclature that I wish to validate is the following:

0100455_LM_Izaguirre


Where "0100455" will generally have 7 to 10 characters, "LM" may vary "LM, LI, AQ" and everything that comes after (eg "Izaguirre") is a text with at least 1 character.
What is important is that scripts are maintained and needed.

All this is necessary to validate a value that will then be modified in PHP and will later be pasted into a Word with PHPWord.

//0100455_LM_Izaguirre
$sitebase = "0100455_LM_Izaguirre";
$string = explode("_", $sitebase);
$idcontrol = $string[1].'_'.$string[0];

Answer

^[0-9]{7,10}_[A-Z]{2}_[A-Za-z]+$ will bee the regex if LM will be any 2 charecters.

^[0-9]{7,10}_(LM|LI|AQ)_[A-Za-z]+$ will bee the regex if LM will only be {LM, LI, AQ}.

Demo:-

var rx = /^[0-9]{7,10}_(LM|LI|AQ)_[A-Za-z]+$/gm;
console.log("0100455_LM_Izaguirre".match(rx));
console.log("0100455_YM_Izaguirre".match(rx));
console.log("0100455_AQ_Izaguirre".match(rx));

null will return for unmatching