简体   繁体   English

正则表达式不适用于JSP Java中的数字

[英]Regular expression is not working for numbers in jsp java

what I want is If the enter text box value is in format or not (1-10 number hyphen number) i did this for one pattern matching /^[\\d]+([-][\\d]+)?$/g but it allows only 0-9(single digits) i not taking 11-20(double digits) I need to allow any type digits like 10-11 and how can i do this 我想要的是如果输入文本框的值是否为格式(1-10个数字连字符),则我对一个匹配/^[\\d]+([-][\\d]+)?$/g but it allows only 0-9(single digits) i not taking 11-20(double digits)我需要允许任何类型的数字,如10-11,我该怎么做

var txt =  document.getElementById("txtShareCount").value;      
        if(txt.match(/^[\d]+([-][\d]+)?$/g)){       
            return true;
            }           
        else if(!txt.match( /^[\d]+([-][\d]+)?$/g)){
            alert("Pleae Enter Share count this(1-10) format");
            document.getElementById("txtShareCount").focus();
            return false;
            }
        else{return true;}

Here i'm posting the OP post as executable snippet to show his code is working perfect . 在这里,我将OP帖子作为可执行代码片段发布,以表明他的代码运行正常。 i said in comment he didn't accept that . 我在评论中说他不接受。 so only i'm posting this snippet . 所以只有我发布此代码段。

1) Additionally i applied only trim() 1)另外我只应用了trim()

 $('#ss').click(function(){ var txt = $("#txtShareCount").val(); if(txt.trim().match(/^[\\d]+([-][\\d]+)?$/g)){ alert("hi valid format "); } else if(!txt.trim().match(/^[\\d]+([-][\\d]+)?$/g)){ alert("Pleae Enter Share count this(1-10) format"); $("#txtShareCount").focus(); return false; } else{ alert("hi valid format "); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="inputt"><input type="text" id="txtShareCount" name="myanswer" class="myinput"></div> <input type="button" id="ss" value="submit" > 

正则表达式^[\\d]+([-][\\d]+)?$完成您所需要的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM