【问题标题】:Javascript Validation Help - Input must equal value of BlueJavascript 验证帮助 - 输入必须等于 Blue 的值
【发布时间】:2012-09-29 15:37:53
【问题描述】:

我的验证适用于空字段。但是,我想知道如何检查输入字段的值,如果不正确则返回 false。

用户必须输入单词“Blue”来填写表格。

输入

<input type="text" id="formcheck" class="contact" maxlength="255" />

当前验证检查

if (f.formcheck.value != 'Blue') { alert('请输入 正确的词。') f。 formcheck.focus() 返回 false; }

任何帮助都会很棒!

【问题讨论】:

  • 您能否更新您的问题并让您尝试做的事情更清楚。还有你失踪的两个;
  • idname 属性不一样,尤其是在表单中。应该是document.getElementById('formcheck').value 。或者给input起个名字,比如name="blue",然后就可以通过form name的方式访问了。

标签: javascript forms validation input


【解决方案1】:

示例代码,我只使用 jquery 进行点击绑定:

<input type="text" id="formcheck" class="contact" maxlength="255" value="Blue"/>
<input type="button" id="clickB" value="click me">

$("#clickB").click(function(){
   if (document.getElementById('formcheck').value != 'Blue') {
       alert('Please enter the correct word.');
       document.getElementById('formcheck').focus();
       return false;
   }
})

jsfiddle example

【讨论】:

    【解决方案2】:

    语法错误,您忘记在每条语句末尾添加;

    if (f.formcheck.value != 'Blue') {
        alert('Please enter the correct word.')
        f.formcheck.focus()
        return false
    }
    

    或以您的方式使用但添加;

    if (f.formcheck.value != 'Blue') { alert('Please enter the correct word.');  f.formcheck.focus();  return false; }
    

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 2015-06-03
      • 2023-03-31
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多