Php 如何使用pregmatch验证没有字符/%\@?在输入字段中,仅A-Za-z0-9./在另一个输入字段中

Php 如何使用pregmatch验证没有字符/%\@?在输入字段中,仅A-Za-z0-9./在另一个输入字段中,php,regex,html,Php,Regex,Html,PHP验证预匹配不起作用 对于元字符,我知道它们很棘手,如果它们在括号[]内,我认为它们会变成文字值而不是通配符,并且您不需要用反冲来逃避 对于$err_useridpmatch,我不理解为什么当我输入有效输入时,错误仍然显示,而对于$err_passpmatch,当我故意输入错误字符串包括?时,没有验证错误消息 <?php if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['submit'])))

PHP验证预匹配不起作用

对于元字符,我知道它们很棘手,如果它们在括号[]内,我认为它们会变成文字值而不是通配符,并且您不需要用反冲来逃避

对于$err_useridpmatch,我不理解为什么当我输入有效输入时,错误仍然显示,而对于$err_passpmatch,当我故意输入错误字符串包括?时,没有验证错误消息

<?php


if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['submit']))):

    $userid = trim($_REQUEST['userid']);
    $password = trim($_REQUEST['password']);
    $err_useridpmatch = '';
    $err_passpmatch = '';

    if ( !(preg_match('/[^\/.%\\@?]/', $userid)) ) :
        $err_useridpmatch = '<div class="error">User id should not have the following characters:/.%\@?</div>';
    endif; // pattern doesn't match

    if ( !(preg_match('/[A-Za-z0-9.\/]/', $password)) ) :
        $err_passpmatch = '<div class="error">Password must only consist of lower or uppercase letters, numbers, full stop or forward slash</div>';
    endif; // pattern doesn't match


endif; //form submitted

?>
______________________________________________________________________________
HTML section:

<div id="form-content"> 


<form name="orderForm" action="<?php echo $_SERVER['PHP_SELF'] ?>" class=""method="post" autocomplete="on">

    <table id="formtable">

    <tbody>
        <br>
        <br>
        <b><p> Please enter your user id, password and click submit</p></b>

        <br>
        <tr>
          <td class="label"><label for="userid">User ID: </label></td>
          <td class="form-input"><input type="text" id="userid" name="userid" value="<?php echo h(@$values['userid']) ?>"/></td>
          <td>
          <?php if (isset($err_useridpmatch)) { echo $err_useridpmatch; } ?></td>

        </tr>
        <br>
        <tr>
          <td class="label"><label for="password">Password: </label></td>
          <td class="form-input"><input type="password" id="password" name="password" value="<?php echo h(@$values['password']) ?>"/></td>
          <td>
          <?php if (isset($err_passwordpmatch)) { echo $err_passwordpmatch; } ?></td>
        </tr>

    </tbody>    
    </table>

        <p class="required" id="required">Required fields are marked with an asterisk (<abbr class="req" title="required">*</abbr>).</p>



       <table id="tablebutton">
            <tr>
                <td class="buttonfunction"><input type="button" onclick="window.close()" id="cancel"  name="cancel"  value="Cancel"/></td>
                <td class="buttonfunction"><input type="submit" id="submit" name="submit" value="Submit"></td>
            </tr>


    <!--          <td class="buttonfunction"><input type="submit" onclick="RegisterValidation()" value="Submit">-->


        </table>


</form>
</div><!--end form content section-->

<pre>
<?php
    if(isset($_POST['submit'])){
        print_r($_POST);
    }
?>        
</pre>  

______________________________________________________________________________
HTML部分:

应验证不能包含
/
%
\
@
的字符串的用户ID正则表达式将被删除

if ( !(preg_match('/^[A-Za-z0-9.\/]*$/', $password)) )
见:

密码正则表达式必须仅由小写或大写字母、数字、句号或正斜杠组成,它要简单得多:

见此:

详细信息

  • 要在常规PHP正则表达式字符串文本中包含反斜杠,需要将用于匹配反斜杠的两个反斜杠中的每一个都加倍,
    “\\\\”
  • ^
    匹配字符串开头,但不在未转义的
    [
  • $
    如果在字符类之外,则匹配字符串的结尾
  • *
    是一个量词,匹配前面模式部分的0个或多个重复(
    abc*
    匹配
    ab
    abcc
    abcccc
    ,等等)。使用
    +
    匹配1个或多个重复

非常感谢Wiktor。您是一个传奇人物。我在php语句中发现错误,其中变量与上面的passpmatch名称不同。请立即修复所有问题!Thx。
if ( !(preg_match('/^[A-Za-z0-9.\/]*$/', $password)) )