Reg Ex不会验证我是否使用#以及其他字符或数字

时间:2016-01-31 15:38:32

标签: javascript php jquery html ajax

这是我之前提出的问题。对不起,但我检查所有链接提供它没有帮助。对不起,这是我第一次在这里提问,所以不太清楚如何提问

我在这里再次详细解释:

我有一个输入文本字段。 我使用jquery验证用户在此输入框中输入的输入日期。

我在javascript GET方法中将数据作为参数传递并传递给PHP,并使用简单的REG Ex验证它。它确实在所有帐户中验证。但是,如果我在任何测试用例中添加#,则此验证失败。  我的代码:  输入字段:

<div id="clntFstName" >
  <label for="clnt_fst_name">First Name</label>
  <input type="text" id="clnt_fst_name" name="clnt_fst_name" onBlur="checkFieldValid(this.value, this);" value=""/>
            <div class="msgError"></div>
          </div>

如果您在用户离开字段输入框时调用函数 CheckFieldValid

java脚本:

function checkFieldValid(value, obj) {
    var elem = obj.name;
    $('#' + elem).parent().children('.msgError').html('');
    var $label = $("label[for='" + obj.id + "']").text();
    var $id = obj.id;


    $.getJSON("ajax/registerClient.php?action=checkInputFieldValid&varField=" + value + "&lab=" + $label + "&id=" + $id, function(json) {

        if (json.status.length > 0) {

            $.each(json.status, function() {
                if (this['fail'] == 'fail') {
                    var info = '<div class="warningMsg"> ' + this['message'] + '</div>';
                    $('#' + elem).parent().children('.msgError').html(info);
                    $('#' + elem).focus();
                    $('#' + elem).val("");
                }
                if (this['success'] == 'success') {

                    $('#' + elem).parent().children('.msgError').html('this is success');
                }
            });
            if (json.status == 'empty') {

                $('#' + elem).parent().children('.msgError').html('this is empty');
            }
        }
    });


}

PHP代码:

if($_GET['action'] == 'checkInputFieldValid'){




    if(!empty($_GET['varField'])){

        // this creates dynamic session variables and add values to it.
        $_SESSION[$_GET['id']] = $_GET['varField'];



        if(preg_match('/^[a-zA-Z]+$/',$_GET['varField'])){

                        $txtVar = 'It is a valid '.$_GET['lab'];
                        array_push($validFieldArray, array('success' => 'success', 'message' => $txtVar));
                        echo json_encode(array('status' => $validFieldArray));
                        $errorJScript = 0;

                }else{
                        $txtVar = 'Enter a valid '.$_GET['lab'];
                        array_push($validFieldArray, array('fail' => 'fail', 'message' => $txtVar));
                        unset($_SESSION[$_GET['id']]);// unset the session variable to clear when page refresh
                        echo json_encode(array('status' => $validFieldArray));
                        $errorJScript = 1;

                }

    }



}

我不知道我哪里错了?我按照其他成员的说法完成所有操作当我传递GET请求变量时,我可能是在做Java脚本的错误吗?据,直到...为止 我想我确实做了其他成员告诉我的有关PHP部分的内容。但是当我从Java脚本部分拿走它时,数据可能是错误的吗?正如我用其他值检查它从PHP返回。但是当我把#放在我的输入框中时,IT没有进行AJAX调用,也没有返回JSON,也没有设置会话变量。所以当我将varible作为GET参数传递时它可能不会运行AJAX并且只是不验证所以plz告诉我如何将#作为GET参数传递给我,以便我正确验证PHP中的字段。 Plz帮我放松工作:(

1 个答案:

答案 0 :(得分:0)

您的$.getJSON来电应使用encodeURIComponent()来确保您没有创建错误的网址:

$.getJSON("ajax/registerClient.php?action=checkInputFieldValid&varField=" +
    encodeURIComponent(value) + 
    "&lab=" + 
    encodeURIComponent($label) + 
    "&id=" + 
    encodeURIComponent($id), function(json) {

如果你不这样做,那么#字符将被解释为表示URL的哈希字段的开始,并且将忽略URL的其余部分。