1

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

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

输入

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

当前验证检查

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

任何帮助都会很棒!

4

2 回答 2

0

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

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

​</p>

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

​jsfiddle 示例

于 2012-09-29T15:48:48.453 回答
0

语法错误,您忘记了;每条语句的添加结尾。

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; }
于 2012-09-29T17:38:01.643 回答