javascript - 使用 Bootstrap 按钮验证错误

标签 javascript php jquery html twitter-bootstrap

我是 HTML/PHP/Javascript 和 Bootstrap 框架的新手...

这是我的代码:

<form name="Enquiry" method="post" action="query.php">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<h5>Choose search type:</h5>
<div id="selector" class="btn-group">
    <button type="button" name="Surname" class="btn btn-default active">Surname</button>
    <button type="button" name="IDNumber" class="btn btn-default">ID Number</button>
    <button type="button" name="FileNumber" class="btn btn-default">File Number</button>
</div>
<br>
<input id="searchbox" name="QD" type="text" size="20"  maxlength="30" pattern="/[A-z]/g" title="0-Only alphabets allowed! No special characters or numbers. Please correct!">
<script>
$('#selector button').click(function() {
    $(this).addClass('active').siblings().removeClass('active');
    $getName = this.name;

        switch ($getName) {
            case "Surname":
            document.getElementById('searchbox').value="";
            document.getElementById('searchbox').maxLength="30";
            document.getElementById('searchbox').pattern="/[A-z]/g";
            document.getElementById('searchbox').title="1-Only alphabets and spaces allowed. Please correct!";          
      break;
            case "IDNumber":
            document.getElementById('searchbox').value="";
            document.getElementById('searchbox').maxLength="13";
            document.getElementById('searchbox').pattern="\d+";
            document.getElementById('searchbox').title="2-Only numerical value allowed in the ID field! Please correct";            
      break;
            case "FileNumber":
            document.getElementById('searchbox').value="";
            document.getElementById('searchbox').maxLength="5";
            document.getElementById('searchbox').pattern="\d+";
            document.getElementById('searchbox').title="3-Only numerical value allowed in the file number field! Please correct";           
        }
});
</script>

<input type="submit" class="btn btn-primary" name="search" value="Query">
</form>

完整代码位于: https://jsfiddle.net/o5n5ucm6/7/

我想要实现的目标: 根据用户按下的按钮,我使用 regxp 和 maxlength 等设置输入文本框的验证标准。

我遇到的问题是: 当我点击查询按钮(POST方法)时,即使该值与regxp匹配,它仍然会抛出错误。

有什么帮助和建议吗?

最佳答案

Javascript 允许正则表达式文字,因此您无需引用您的行。

更改此行: document.getElementById('searchbox').pattern="/[A-z]/g";

对此: document.getElementById('searchbox').pattern=\[A-z]\g;

这两行: document.getElementById('searchbox').pattern="\d+";

需要改成这样: document.getElementById('searchbox').pattern=\d+;

关于javascript - 使用 Bootstrap 按钮验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42187758/

相关文章:

javascript - 使用全日历以 3 天的事件 block 跳过周末和拆分日期

php - 公司 vs 员工 ID 困境

php - 如何修复这个错误 Package phpunit/phpunit-mock-objects is disabled, 你应该避免使用它。没有建议更换

javascript - JQuery插件函数调用

javascript - DynamoDB batchwriteItem 不会将数据放入 Lambda 函数中的动态 TableName

javascript - Kendo Grid 可编辑弹出窗口是否有某种 IsChanged 事件?

javascript - 使用 jQuery 组织多个嵌入代码

php - 如何使用 guzzleHttp 创建请求?

javascript - 如何获取 "file input"所选文件的长度

javascript - 当浏览器小于 640px 时,如何将列表项转换为 slider ?