Home Web Front-end JS Tutorial js determines whether the content entered in the text box is a number_javascript skills

js determines whether the content entered in the text box is a number_javascript skills

May 16, 2016 pm 03:24 PM
js number text box

如何验证文本框中的内容是否为数字,本文提供了三种方法,希望对大家的学习有所启发。

在某些情况下可能需要让文本框中的内容只能够输入数字,例如手机号码或者邮编之类的,下面简单介绍一下如何实现此功能。
下面是验证数字的正则表达式:

"^\\d+$"          //非负整数(正整数 + 0) 
"^[0-9]*[1-9][0-9]*$"    //正整数 
"^((-\\d+)|(0+))$"     //非正整数(负整数 + 0) 
"^-[0-9]*[1-9][0-9]*$"   //负整数 
"^-?\\d+$"         //整数 
"^\\d+("           //非负浮点数(正浮点数 + 0) 
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"    //正浮点数 
"^((-\\d+("         //非正浮点数(负浮点数 + 0) 
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"   //负浮点数 
"^(-?\\d+)("         //浮点数
Copy after login

用js判断文本框输入的内容是否是数字:

<script language="javascript">
 function chkads()
 {
 if (lf_addstu.sname.value=="")
 {
 alert("姓名不能为空.");
 lf_addstu.sname.select();
 return false;
 }
 if (lf_addstu.tpl.value=="")
 {
 alert("联系电话不能为空.");
 lf_addstu.tpl.select();
 return false;
 }
 if(!IsNum(lf_addstu.tpl.value)){
 alert("请输入数字!")
 lf_addstu.tpl.focus();
 return false;
 }
 }
 function IsNum(num){
 var reNum=/^\d*$/;
 return(reNum.test(num));
}
</script>
 <form style="padding:0; margin:0" action="" method="post" onSubmit="return chkads()" id="lf_addstu" name="lf_addstu">
 姓名<br />
 <input name="sname" type="text" /><br />
 详细地址<br />
 <input name="adress" type="text" /><br />
 邮编<br />
 <input name="codes" type="text" /><br />
 联系电话<br />
 <input name="tpl" type="text" /><br />
 E-mail<br />
 <input name="email" type="text" />
 <input type="image" src="img/wanhui06.jpg" alt="提交" />
 </form>
Copy after login

如何判断输入文本框是值是否是数字?

单纯的判断是否是正整数,可使用char.IsDigh(string,int index)和IsNumber(string,int index)函数

protected void Button2_Click(object sender, EventArgs e)
 {
  //判断正整数
  int j=0;
  for (int i = 0; i < TextBox1.Text.Length; i++)
  {
   if (char.IsNumber(TextBox1.Text, i))//这个方法用来判断整数还可以,判断负数和小数就失效了
    j++;
  }
  if (j == TextBox1.Text.Length)
  {
   Response.Write("ok");
  }
  else
  { Response.Write ("no");}
  
 }
Copy after login

但是,出现负数或者小数的时候,以上方法失效,则,使用自定义功能函数

public bool IsNumber( object obj) 
 { 
 bool result = true; 
 try 
  { 
   string str = obj.ToString(); 
   double d ; 
   d = double.Parse(str); 
  } 
 catch 
  { //parse 函数进行转换,不成功则抛出异常
   result = false; 
  } 
 return result;

 }
 protected void Button3_Click1(object sender, EventArgs e)
 {
  //判断数

  if (IsNumber(TextBox1.Text))
  {
   Response.Write("是数字");
  }
  else
  { Response.Write("不是数字"); }
 }
Copy after login

以上就是验证文本框中的内容是否为数字的方法,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Realme GT Neo6 is scheduled to be released on May 9th! The first AI digital human conference in the computer industry Realme GT Neo6 is scheduled to be released on May 9th! The first AI digital human conference in the computer industry May 08, 2024 pm 12:49 PM

On May 7, our mobile phone manufacturer officially announced that our company’s GTNeo6 launch conference is scheduled for May 9. GTNoe6 is positioned as a "performance storm", aiming to stir up the mid-range machine situation. In addition, this conference will also be the first AI digital human conference in the mobile phone industry. At that time, Realme Vice President, Global Marketing President, and China President Xu Qi will appear at the press conference in the form of a digital human. Digital man Xu Qi According to the official introduction, Realme GTNoe6, codenamed "Hurricane", is faster and stronger. It will challenge the strongest third-generation Snapdragon 8s flagship and the strongest product in its class. Recently, the Realme GTNeo6 was found to be directly on the e-commerce platform. Some core configurations were exposed, showing that the machine is not only equipped with a Snapdragon 8s processor, but also supports 120W flash charging.

js method to refresh current page js method to refresh current page Jan 24, 2024 pm 03:58 PM

js methods to refresh the current page: 1. location.reload(); 2. location.href; 3. location.assign(); 4. window.location. Detailed introduction: 1. location.reload(), use the location.reload() method to reload the current page; 2. location.href, you can refresh the current page by setting the location.href attribute, etc.

How to restore WeChat corner mark numbers How to restore WeChat corner mark numbers Nov 29, 2023 pm 05:46 PM

Methods to restore the WeChat corner number: 1. Force quit WeChat and restart; 2. Clear the WeChat cache; 3. Check for WeChat version updates; 4. Uninstall and reinstall WeChat. Detailed introduction: 1. Force quit WeChat and restart. This is the most common method to solve the abnormal number of WeChat corner mark. In the WeChat interface, click the "Me" button in the lower left corner, and then click the "Settings" button in the upper right corner. Open the settings interface. In the settings interface, select "Log out" to log out of WeChat. After a few seconds, start WeChat again. Normally, the corner number will return to normal, etc.

See all articles