javascript中的RegEx仅适用于数字 [英] RegEx in javascript for only numbers

查看:67
本文介绍了javascript中的RegEx仅适用于数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有文本框(最大长度为2),该文本框应只包含1-25之间的数字.除空格,字符,-ve值或字母等数字外,我已编写代码将文本框背景更改为黄色,如下所示

I''m having a page with text boxes (with maxlength 2)which should take only numbers from 1-25. Anything except numbers like spaces, characters, -ve values or alphabets etc, i''ve written code to change the textbox background to yellow as below

$("#divBingoKeyIn input:text").each(function () {
                   var currentValue = $(this).attr("value");
                   if (currentValue == "" || parseInt(currentValue) > 25 || parseInt(currentValue) < 1) {
                       $(this).css("background-color", "yellow");
                       isValuesValid = false;
                   }
                   else {
                       $(this).css("background-color", "White");
                   }



但是对于上面的代码,我使用了原始方法来查找它是否不是数字.

我想用下面的简单代码替换它



But for above code i''ve used primitive methods to find if it is not a number.

i would like to replace this with a simple code like below

$("#divBingoKeyIn input:text").each(function () {
                    var currentValue = $(this).attr("value");
                    if (currentValue.match(regex)) {
                        $(this).css("background-color", "yellow");
                        isValuesValid = false;
                    }
                    else {
                        $(this).css("background-color", "White");
                    }



有人可以为我提供上述情况的正则表达式吗?



Can anybody please give me a regex for the above scenario?

推荐答案

(" ).each( function (){ var currentValue =
("#divBingoKeyIn input:text").each(function () { var currentValue =


( this ).attr(" ); 如果(currentValue == " | | parseInt (currentValue)> 25 || parseInt (currentValue)< 1 ){
(this).attr("value"); if (currentValue == "" || parseInt(currentValue) > 25 || parseInt(currentValue) < 1) {


(> ).css(" " ); isValuesValid = false ; } 其他 {
(this).css("background-color", "yellow"); isValuesValid = false; } else {


这篇关于javascript中的RegEx仅适用于数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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