简体   繁体   English

如何使用javascript检查文本框中的第一个字符是否为数字?

[英]how to use javascript to check if the first character in a textbox is a number?

I'm making a simple form and having a textbox for street address.... All I want to do is check if the first value entered is a number or not. 我正在制作一个简单的表格,并有一个街道地址的文本框....我想要做的就是检查输入的第一个值是否是一个数字。

How can I do it? 我该怎么做?

if(document.forms[0].elements[2].value.

that is all I have now but I'm not sure what I should add to it to check the first character only. 这就是我现在所拥有的一切,但我不确定我应该添加什么才能检查第一个字符。

As you said in your question you want to check for the first character only, you can use charAt function for string to check whether the first character is from 0 to 9 or any other check you want for the first character 正如您在问题中所说的那样,您只想检查第一个字符,您可以使用字符串的charAt函数来检查第一个字符是从0到9还是第一个字符所需的任何其他检查

Possible solution 可能解决方案

var firstChar = document.forms[0].elements[2].value.charAt(0);
if( firstChar <='9' && firstChar >='0') {
      //do your stuff
}

This can simply use isNaN . 这可以简单地使用isNaN Just put a bang before it to check if it is a number, instead of isNaN 's normal use of checking if it isn't a number, like so: 只是在它前面检查它是否是一个数字,而不是isNaN正常使用检查它是否不是一个数字,如下所示:

var val = document.forms[0].elements[2].value;
if (!isNaN(val.charAt(0))){ //If is a number
  //Stuff
}

This also goes with numbers as strings, so need to worry about quotes or any of that hoo ha. 这也是数字作为字符串,所以需要担心引用或任何hao ha。

You can use if (document.forms[0].elements[2].value.match(/^\\d+/)) to check if the beginning of the field is composed by numbers. 您可以使用if (document.forms[0].elements[2].value.match(/^\\d+/))来检查字段的开头是否由数字组成。

It will match for: 它将匹配:

0 - valid
1 - valid
1a - valid
1 a - valid
1234567 - valid
a - invalid
a1 - invalid

Literally anything that start with numbers. 字面上任何以数字开头的东西。

You can extend its functionality to if (document.forms[0].elements[2].value.match(/^\\d+ +.+/)) 您可以将其功能扩展到if (document.forms[0].elements[2].value.match(/^\\d+ +.+/))

In this form it will now require that its a number, plus one or more spaces, followed by anything else. 在这种形式下,它现在需要一个数字,加上一个或多个空格,然后是其他任何空格。

0 - invalid
1 - invalid
1(space) - invalid
1 1 - valid
1 a - valid
12345 abcdef - valid

Read more about Regular Expressions to elaborate complexier checkings. 阅读有关正则表达式的更多信息,以详细说明复杂的检查。

But remember first that not every address has numbers, and most countries in the world don't use this format of writing addresses. 但请记住,并非每个地址都有数字,世界上大多数国家都不使用这种写地址格式。 As for the address field, I believe you should leave it open to be written in however format the user wish. 至于地址字段,我相信你应该保持开放状态,无论用户希望的格式如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中的一个文本框中验证字符和数字 - how to validate character and number in one textbox in javascript Javascript 检查第一个字符是数字、大写、小写还是特殊字符 - Javascript check if first character is number, upperCase, lowerCase or special character 如何在javascript中验证文本框的第一个字符是Alpha,第二个字符是数字? - How to validate the textbox first character is Alpha and second character numeric in javascript? 如何使用 javascript HTML 检查文本框中的数字? - How to check for a number in a textbox using javascript HTML? 如何识别字符串中的第一个字符是否是Javascript中的数字 - How to identify if first character in a string is a number in Javascript 如何检查javascript中的字符是字母还是数字? - How to check if a character is a letter or a number in javascript? 如何检查字符串的第一个字符是否是数字作为 *ngIf 中的条件 - How can I check if the first character of a string it is a number as a condition in *ngIf 如何使用javascript检查HTML输入文本框是否在某个数字以下? - How to check if HTML input textbox is below a certain number using javascript? Javascript如何更改文本框上的字符? - Javascript how to change character on textbox? 如何检查文本框值是否不是数字? - How to check if textbox value is not number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM