用于验证文本框的JavaScript [英] javascript for validating textbox

查看:73
本文介绍了用于验证文本框的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的大家,

我有一个文本框,需要进行如下验证:

1.它只能接受12个字符.
2.前两个字母应为"BA".
3.接下来的10个字母应为整数(数字)值.

我需要在textchanged事件中对其进行验证..任何人都可以帮我吗?


问候,
Raj

Hi Dear All,

I have a textbox which needs to be validated as below:

1. It should accept only 12 characters.
2. First two letters should be ''BA''.
3. Next 10 letters should be integer(digit) values.

I need to validate it in the textchanged event.. Can any one plz help me on this?


Regards,
Raj

推荐答案

您可以使用以下代码
对于
You could used following codes
For
1. It should accept only 12 characters.<br />


if ((form_name.text_name.value.length > 12))
{
   Alert("NAme shold less than 12 characters");
}


2. First two letters should be ''BA''


2. First two letters should be ''BA''

if( form_name.text_name.value.charAt(0)!=''A''||form_name.text_name.value.charAt(1)!=''B'')
{
   Alert("invalide value");
}


是,第三是您的作业.


and Yes Third is Your homework.


1)将文本"框设置为仅包含12个值.

为什么不限制用于修改< input>大小的文本?文本框使用的年份?
1 ) Setting the Text box to take only 12 Values.

Why dont you limit the text for size modifying <input> tage used for the text box ?
<input type="text" size="value" maxlength="12"> </input>



2)前2个应该是BA.

您可以使用Substring运算符和字符串比较来实现此目的.
用值BA初始化actualString.



2) First 2 should be BA.

You can use Substring operator and string comparison to achieve this.
Initialise actualString with value BA.

if ( stringSource.substring(0,1).toString() != actualString.toString() )
  {
      // Display Error
  }



3)检查其余10位数字是否为数字.



3) Check the rest of the 10 digits are number.

if ( isNaN(stringSource.substring(2,11)))
  {
     // Display Erro
  }



您可以将以上内容添加到一个函数中,然后将其设置为可用于textchanged事件.

BR//
Harsha</input>



You can add the above contents into one function and then set it to work for the textchanged event.

BR//
Harsha</input>


使用正则表达式进行检查.很简单
这是您可以使用的功能

Use regular expression to check. It''s simple
here''s a function you can use

function Check()
{
 var ex = /^BA[0-9]{10}


这篇关于用于验证文本框的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆