简体   繁体   中英

Compilation failed: range out of order in character class at offset 12

I have a very basic HTML form with text boxes defined as

<li id="li_1" >
<label class="description" for="element_1">O teu nome </label>
<span>
<input id="element_1_1" name= "element_1_1" class="element text" pattern="[a-z0-9. -]+" maxlength="255" size="8" value=""/>
<label>Primeiro</label>
</span>
<span>
<input id="element_1_2" name= "element_1_2" class="element text" pattern="[a-z0-9. -]+" maxlength="255" size="14" value=""/>
<label>Apelido</label>
</span> 

This input is validated in a php file called email_send_pt.php using

// validation expected data exists
if(!isset($_POST['element_1_1']) ||
!isset($_POST['element_1_2']) ||
!isset($_POST['element_2']) ||{
died('Lamentamos, mas constam erros no seu registo.'); 
}

$first_name = $_POST['element_1_1']; // required
$second_name = $_POST['element_1_2']; // required
$email = $_POST['element_2']; // required

$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name) ||
!preg_match($string_exp,$second_name)) {
$error_message .= 'O nome que preencheu não parece ser válido.<br />';
}

I am getting a constant "Compilation failed: range out of order in character class at offset 12 in email_send_pt.php on line 45"

Line 45 is "if(!preg_match($string_exp,$first_name) ||" As input I used "teu" (without "") which should be accepted…

Any idea why this error message is appearing ? Thank you for your help.

You have to close brace here and remove the || operator.

Also it is die() and not died()

died('Lamentamos, mas constam erros no seu registo.'); 

Putting it all together

if(!isset($_POST['element_1_1']) ||
    !isset($_POST['element_1_2']) ||
    !isset($_POST['element_2'])){
    die('Lamentamos, mas constam erros no seu registo.');
}

The message means that there is an error at character 12 in your regex. Since that is the apostrophe (single quote), my guess is that you need to escape it.

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