简体   繁体   English

如何使用Java脚本验证jsp中的文本字段以仅接受点和逗号作为特殊字符

[英]how to validate a text field in jsp using java script for accepting only dot and comma as special characters

I need to allow only numerics, dot and comma in a text field. 我需要在文本字段中仅允许数字,点和逗号。 If the user enters other special characters an alert needs to be thrown. 如果用户输入其他特殊字符,则需要引发警报。

Please find below the conditions: 请找到以下条件:

  1. The text field should contain atleast one numeric value. 文本字段应至少包含一个数字值。
  2. It is not mandatory that the text field should contain dot and comma always. 并非强制要求文本字段必须始终包含点和逗号。

Thanks for your help. 谢谢你的帮助。

Here is a start for you: http://jsfiddle.net/KLyy8/1/ 这是您的起点: http : //jsfiddle.net/KLyy8/1/

Unless you tell us the exact specifics we won't know how to write the regular expression. 除非您告诉我们确切的细节,否则我们将不知道如何编写正则表达式。 Sounds like you want numbers with thousands separator and decimals. 听起来像您想要带千位分隔符和小数的数字。 But how many of each? 但是每一个呢? The fiddle above will work for numbers like this... 上面的小提琴将适用于这样的数字...

123,456.79 123,456.79

12,345.6 12,345.6

1,234.56 1,234.56

etc... 等等...

validate=function(){
    var str = document.getElementById('test').value;
    var patt = new RegExp("^[0-9]{1,3}\,[0-9]{3}\.[0-9]{1,2}$");
    var res = patt.test(str);
    alert(res);
}

If the field should contain only numeric values with decimal places (for exmaple 3.14 , 112358 or 9,81 ), then you can use only one regex: 如果该字段应包含小数位(对于〔实施例仅数值3.141123589,81 ),那么你可以只用一个正则表达式:

function validateString(str){
    if(str.match(^[0-9\.,]+$)){
        return true;
    }else{
        return false;
    }

However if you want to be precise you will probably want to limit the dots and commas to one, if I understand your situation correctly. 但是,如果您想精确一点,如果我正确理解您的情况,则可能需要将点和逗号限制为一个。

Regex for that can look like this 正则表达式可以看起来像这样

^[0-9]+(\.|,){0,1}[0-9]+$

That means it starts with a number ( ^[0-9]+ ), then it may contain zero or one times one comma or dot ( (\\.|,){0,1} ) and then again ends with a number ( [0-9]+$ ). 这意味着它以数字( ^[0-9]+ )开头,然后可能包含零或一个逗号或点( (\\.|,){0,1} )的一倍,然后再次以数字( [0-9]+$ )。

暂无
暂无

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

相关问题 Javascript:如何验证只有字母数字的文本字段,没有特殊字符或逗号 - Javascript: how to validate a text field which is alphanumeric only, without special characters or comma 如何使用Java脚本验证正则表达式中jsp中的文本字段 - how to validate Regex for text fields in jsp using java script 仅允许逗号后的空格并使用正则表达式限制文本字段中的特殊字符 - Only allow whitespace after comma and restrict special characters in text field using regex 如何使用与Java脚本特殊符号,字符和数字正则表达式来验证密码 - How to Validate the password using regular expression with Special Symbols, Characters and Numeric in Java Script 如何使用Java脚本验证html文本字段以确保文本字段中包含的单词不超过一个 - how to validate html text field using java script to ensure no more than one word contain in text field 如何限制输入框接受Angular中的特殊字符? - How to restrict input field from accepting special characters in Angular? 使用livevalidation脚本 - 除_(下划线) - (连字符)之外的特殊字符。 (点)和空间 - using livevalidation script - special characters except _ (underscore) - (hyphen) . (dot) and Space 如何测试文本字段验证字段中是否存在逗号 - How to test Text Field Validate a comma exists in a field 如何使用jQuery正则表达式验证字符,数字和特殊字符? - How to validate characters, numbers and special characters using jQuery regular expression? 使用 jQuery 验证接受数字和特殊字符 - Validation for accepting numbers and special characters using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM