如何过滤所有特殊字符(旁边的下划线)以显示允许和不允许?

时间:2014-06-04 13:27:33

标签: javascript php

如何筛选所有特殊字符(旁边的下划线)以显示允许和不允许?

根据我的意愿,我将只允许文字,数字和下划线

但是当我测试此代码时,我会尝试在输入中填写123456abcd&,它会显示

123456abcd& available如何过滤所有特殊字符(旁边的下划线)

的index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script language="JavaScript">
       var HttPRequest = false;

       function doCallAjax() {
          HttPRequest = false;
          if (window.XMLHttpRequest) { // Mozilla, Safari,...
             HttPRequest = new XMLHttpRequest();
             if (HttPRequest.overrideMimeType) {
                HttPRequest.overrideMimeType('text/html');
             }
          } else if (window.ActiveXObject) { // IE
             try {
                HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          } 

          if (!HttPRequest) {
             alert('Cannot create XMLHTTP instance');
             return false;
          }

          var url = 'check_char.php';
          var pmeters = "CUsername=" + encodeURI( document.getElementById("CheckUsername").value);
            HttPRequest.open('POST',url,true);
            HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            HttPRequest.setRequestHeader("Content-length", pmeters.length);
            HttPRequest.setRequestHeader("Connection", "close");
            HttPRequest.send(pmeters);
            HttPRequest.onreadystatechange = function()
            {

                if(HttPRequest.readyState == 3)  // Loading Request
                {
                    document.getElementById("mySpan").innerHTML = "..";
                }
                if(HttPRequest.readyState == 4) // Return Request
                {
                    if(HttPRequest.responseText == 'Y')
                    {
                        window.location = 'AjaxPHPRegister3.php';
                    }
                    else
                    {
                        document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
                    }
                }       
            }
       }
    </script>


        <input size="30" tabindex="1" type="text" name="CheckUsername" id="CheckUsername" onchange="JavaScript:doCallAjax();" 
         value="<?php if(isset($_POST['CheckUsername'])) echo $_POST['CheckUsername']; ?>"> 
        <span id="mySpan"></span>

check_char.php

<?php
$strUsername = $_POST[CUsername];
            $special_char_total = strlen(preg_replace('@[a-z0-9_\-\.]@', '', $strUsername));
            $text_and_number_char_total = strlen(preg_replace('@[^a-z0-9]@', '', $strUsername));

            if ($special_char_total > 0)
                { echo "Username can only contain letters, numbers and underscores (and no spaces!)"; }
            elseif ($text_and_number_char_total < 1)
                { echo "Username must have at lest 1 letter or number"; }
            else
                {
                    echo"$_POST[CUsername] available.";
                }
?>

1 个答案:

答案 0 :(得分:0)

我要做的是检查允许的字符并计算它们。看看以下是否适合您:

<?php
$strUsername = $_POST[CUsername]; 
$strlen =  strlen($strUsername);          
$valid_char_count = strlen(preg_replace('/[^a-zA-Z0-9_]/', '', $strUsername));

if (($valid_char_count < $strlen) || $valid_char_count < 1) {
 if($valid_char_count < $strlen) {
    echo "Username can only contain letters, numbers and underscores (and no spaces!)";
 }

 if ($valid_char_count < 1) { 
    echo "Username must have at lest 1 letter or number"; 
    }
} else {
    echo $strUsername." available";
}
?>