1

例如,如果我有一个表单并且我不希望用户在其中输入数字并且我使用包含正则表达式的函数对其进行验证,我如何防止用户输入的无效字符(在此示例中为数字) 如果它没有通过正则表达式测试,它会不会出现在文本表单中?

这是我尝试过的功能和我尝试过的选择列表(换句话说,这不是整个程序)。我尝试将 false 返回到 onkeypress 事件处理程序,但用户输入到文本框中的内容仍然通过。

function noNumbers(answer) {  //returns false and displays an alert if the answer contains numbers
     if (/[\d]+/.test(answer)) {    // if there are numbers
         window.alert("You can not enter numbers in this field");
         return false;
     }

}

<form action="get" enctype="application/x-www-form-urlencoded">
    <select id="questions" name="questions">
         <option value="no_numbers">What is the name of the city where you were born?</option>
         <option value="no_letters">What is your phone number?</option>
         <option value="no_numbers">What is the name of your favorite pet?</option>
         <option value="no_letters">What is your social security number?</option>
         <option value="no_numbers">What is your mother's maiden name?</option>  
    </select>
    <p><input type="text" name="answer" onkeypress="validateAnswer();" /></p>
</form>
4

4 回答 4

1

当您在相关字段中输入无效字符时,此验证非常适合即时去除无效字符。例子:

<form id="form1" name="form1" method="post">
  Email:
  <input type="text" name="email" id="email" onkeyup='res(this, emailaddr);' ; </form>

  <script>
    var phone = "()-+ 0123456789";
    var numb = "0123456789";
    var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ @-'.,";
    var alphanumb = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ @-.'1234567890!?,:;£$%&*()";
    var alphaname = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ,-.1234567890";
    var emailaddr = "0123456789@._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    function res(t, v) {
      var w = "";
      for (i = 0; i < t.value.length; i++) {
        x = t.value.charAt(i);
        if (v.indexOf(x, 0) != -1)
          w += x;
      }
      t.value = w;
    }
  </script>

然后,您只需使用代码中定义的变量将 javascript 调用的第二个值更改为您希望在字段中输入的数据类型。

于 2021-08-10T16:00:58.507 回答
0

通过向您的函数添加一个额外的参数来简化您的生活,validateAnswer如下所示:
<input type="text" id="answer" name="answer" onkeyup="validateAnswer(this);" />

然后你可以这样定义你的validateAnswer

function validateAnswer(elem){
    elem.value = elem.value.replace(/[^\d]/g, '');
}

这里是一个例子:http: //jsbin.com/iwiduq/1/

于 2013-03-31T08:21:23.560 回答
0

您从传递给处理程序的事件对象中获得被按下的键。

input type="text" name="answer" onkeypress="validateAnswer(this, event);" />

function validateAnswer(element, event) {
  if (event.charCode) {
    if (/\d/.test(String.fromCharCode(event.charCode))) {
      window.alert("You can not enter numbers in this field");
      return false;
    }
  }
}

谷歌搜索“onkeypress 事件”可以找到很多这样的例子。

于 2013-03-31T08:27:24.477 回答
0

这是您正在寻找的功能

function validateAnswer(src) {
    var questions = document.getElementById("questions");
    var rule = questions.options[questions.selectedIndex].value;
    if(rule=="no_numbers") src.value = src.value.replace(/\d/g, '');
    if(rule=="no_letters") src.value = src.value.replace(/\w/g, '');
}

只需将输入字段引用发送到函数并将其设置为onkeyup事件:

<input type="text" name="answer" onkeyup="validateAnswer(this);" />

您还应该挂钩选择框的onchange事件以重置输入框的值。我建议你也考虑HTML5 pattern attribute. 看

于 2013-03-31T09:01:57.073 回答