JavaScript 中数字字符的表单验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5795612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:22:19  来源:igfitidea点击:

Form validation of numeric characters in JavaScript

javascript

提问by Igor Novoselov

I would like to perform form validation using JavaScript to check for input field only to contain numeric characters.So far, the validation checks for the field not being empty - which works fine.However, numeric characters validation is not working.I would be grateful for any help.Many thanks.

我想使用 JavaScript 执行表单验证以检查仅包含数字字符的输入字段。到目前为止,该字段的验证检查不是空的 - 工作正常。但是,数字字符验证不起作用。我将不胜感激任何帮助。非常感谢。

<script type="text/javascript">
//form validation
function validateForm()
{
var x=document.forms["cdp_form"]["univer_number"].value
if (x==null || x=="")
  {
  alert("University number (URN) field must be filled in");
  cdp_form.univer_number.focus();
  return false;
  }
else if (is_valid = /^[0-9]+$/.test(x))
    {
    alert("University number (URN) field must have numeric characters");
    cdp_form.univer_number.focus();
    return false;
  }
}
</script>


<input type ="text" id="univer_number" maxlength="7"    size="25"   name="univer_number" />

采纳答案by mVChr

You need to test for the negation of the RegExp because you want the validation to alert upon failure, so just add !in front of it:

您需要测试 RegExp 的否定,因为您希望验证在失败时发出警报,因此只需!在其前面添加:

else if (is_valid = !/^[0-9]+$/.test(x))

See example →

参见示例 →

回答by Oded

Your test condition is a bit strange:

你的测试条件有点奇怪:

else if (is_valid = /^[0-9]+$/.test(x))

Why have the redundant comparison to is_valid? Just do:

为什么有多余的比较is_valid?做就是了:

else if (/^[0-9]+$/.test(x))

Though the regex you are using will match numerals and only numerals - you need to change it to match anything that is nota numeral - like this /^[^0-9]+$/.

尽管您使用的正则表达式将匹配数字并且仅匹配数字 - 您需要更改它以匹配任何不是数字的东西- 就像这样/^[^0-9]+$/

Better yet, get rid of the regex altogether and use IsNumeric:

更好的是,完全摆脱正则表达式并使用IsNumeric

else if (!IsNumeric(x))

回答by Mikecito

Rather than using Regex, if it must only be numerals you can simply use IsNumeric in Javascript.

而不是使用正则表达式,如果它必须只是数字,你可以简单地在 Javascript 中使用 IsNumeric。

IsNumeric('1') => true; 
IsNumeric('145266') => true;
IsNumeric('abc5423856') => false;

回答by Egor

You need invert your regular expression (add ^ inside [0-9]):

您需要反转正则表达式(在 [0-9] 内添加 ^):

/^[^0-9]+$/

回答by Don

Your pattern will still accept this input <b>#@$@#123or ad!@#12<b>. Use this pattern I created:

您的模式仍将接受此输入<b>#@$@#123ad!@#12<b>. 使用我创建的这个模式:

/[a-zA-Z-!@#$%^&*()_+\=\[\]{};':"\|,.<>\/?]/ 

This pattern will check if it is alphabetic and special characters.

此模式将检查它是否是字母和特殊字符。

回答by Donnie

On your line that says else if (is_valid = /^[0-9]+$/.test(x)), you're doing a simple assignment instead of testing that it is actually matching the regex.

在你说的那一行else if (is_valid = /^[0-9]+$/.test(x)),你正在做一个简单的分配,而不是测试它实际上是否与正则表达式匹配。

回答by Dorothy Kerrin Ryan

I know this is an old post but I thought I'd post what worked for me. I don't require the field to be filled at all but if it is it has to be numerical:

我知道这是一个旧帖子,但我想我会发布对我有用的内容。我根本不需要填写该字段,但如果是,则必须是数字:

function validateForm()
{
 var x=document.forms["myformName"]["myformField"].value;

if (/[^0-9]+$/.test(x))
    {
    alert("Please enter a numerical amount without a decimal point");
    myformName.myformField.focus();
    return false;
  } 
}