javascript验证,仅输入四个数字 [英] javascript validation for enter only four number

查看:76
本文介绍了javascript验证,仅输入四个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

所有人,

我需要对我的文本框进行验证,该验证仅接受四个整数.如果用户仅输入两个或四个以上的数字,则会输入错误的数字.

因此,如果使用输入四个数字,则表示接受.

如果用户输入三个数字和一个空格,则还会输入错误的数字..

hi

to all,

i need a validation to my textbox, that accept only four integer. if user enter only two number or more than four it would say wrong number enter.

so if use enter four number it accept.

If user enter three number and one space than also it would raise message wrong number enter..

thanks!

推荐答案

通过正则表达式使用此验证.

[0-9] [0-9] [0-9] [0-9]
Use this validation using regex.

[0-9][0-9][0-9][0-9]


尝试此链接

http://bytes.com /topic/javascript/answers/90985-validate-form-4-digit-integer [
try this link

http://bytes.com/topic/javascript/answers/90985-validate-form-4-digit-integer[^]

you can also use this code and add only one or 2 line of code according to you requiremnest


<script type ="text/javascript">
    function MaskMoney(evt)
    {
    if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57))) return false;

    var parts = evt.srcElement.value.split('.');

    if (parts.length > 4) return false;
    if (evt.keyCode == 46) return (parts.length == 1);
    if (parts[0].length >= 14) return false;
    if (parts.length == 4 && parts[1].length >= 4) return false;
    }
    </script>


In aspx.cs page
-----------------

 protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Attributes.Add("onKeydown", "return MaskMoney(event)");
        }


You can use \s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s* expression for allow spaces.


这篇关于javascript验证,仅输入四个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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