正则表达式验证姓氏问题

时间:2016-03-24 16:58:24

标签: php regex validation

我试图在将表单插入db之前验证它。 名字,姓氏和电子邮件地址。电邮地址工作正常。这是我遇到问题的姓氏,这也意味着我遇到了第一个名字的问题。

第一个和最后一个名字允许任何字符,包括数字和符号。我哪里出错?

  if (preg_match('/^[A-Z \'.-]{2,45}$/i', $_POST['lname'])) {
    $c_lname = escape_data($_POST['lname'], $dbc);
} else {
    $c_lnameError = ("Please enter your last name!");
}

这是我的验证,我正在尝试使用正则表达式来坚持该值介于2到45个字符之间,并且只包含大写和小写字母,空格,句点,撇号和连字符的组合。 任何帮助将不胜感激。

PHP

<?php
 $c_fnameError = $c_lnameError = $c_emailError = $c_phoneError = "";

if (isset($_POST['Update'])) {
$c_fname = $_POST['fname'];
$c_lname = $_POST['lname'];
$c_email = $_POST['email'];
$c_phone = $_POST['phone'];   
//validate laste name
if (preg_match('/^[A-Z \'.-]{2,45}$/i', $_POST['lname'])) {
    $c_lname = escape_data($_POST['lname'], $dbc);
} else {
    $c_lnameError = ("Please enter your last name!");
}
// validate email
if (!filter_var($c_email, FILTER_VALIDATE_EMAIL) === false) {       
        } else {
           $c_emailError = ("<b> Email is not a valid email address</b>");
        }      
$insert_det = "INSERT INTO Cus_acc_details(CUS_Fname,CUS_Lname,Cus_Email,CUS_Phone) 
    VALUES (?,?,?,?)
    ON DUPLICATE KEY 
    UPDATE
    Cus_acc_details.CUS_Fname = '$c_fname',
    Cus_acc_details.Cus_Lname = '$c_lname',
    Cus_acc_details.Cus_Email = '$c_email',
    Cus_acc_details.CUS_Phone = '$c_phone'";
    $stmt = mysqli_prepare($dbc, $insert_det);
   mysqli_stmt_bind_param($stmt, 'sssi', $c_fname, $c_lname, $c_email, $c_phone);
/* execute query */
$r = mysqli_stmt_execute($stmt);
if ($r) {
    echo "<script> alert('Saved')</script>";
} else {
    echo "<b>Oops! we have an issu </b>";
}
}
?>

HTML

<form id="myform " class="Form" method="post" action="Cus_Account.php?c_id=<?php echo $c_id ?>" accept-charset="utf-8">

    <!--                    <div id="first">-->
        <input type="text" id="fname" name="fname" value="<?php echo $_SESSION['fname']; ?>" required> 
    <input type="text" id="lname" name="lname" value="<?php echo $_SESSION['lname']; ?>"  required>
    <input type="text" id="email" name="email" value="<?php echo $_SESSION['Cus_Email']; ?>" required>
    <!-- Onclick only accept numbers between 0-9-->
    <input type="tel" id="phone" name="phone"  value="<?php echo $_SESSION['phone']; ?>"  onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" required>
    <!-- id = phone error messge-->
    <span id="error" style="color: Red; display: none">Oops! I can only accept numbers</span>
    <input type="submit" name="Update" value="Update">
    <br>
</form>

0 个答案:

没有答案