文本框应验证百分比值 [英] textbox should validate for percentage value
本文介绍了文本框应验证百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证Java脚本中文本框按键事件中的百分比值.
百分比值不应超过100,小数点不应超过1
i want validate percentage value in textbox keypress event in java script.
percentage value should not go more than 100 and decimal point should not press more than 1
推荐答案
由于验证不是那么复杂,您可以执行各种方式:
方式1:
使用直接验证检查
Since it''s not that complex a validation you can do you can do it various ways:
WAY 1:
using direct validation check
function ValidateValue(str)
{
var x = parseFloat(str);
if (isNaN(x) || x < 0 || x > 100)
{
// alert("value is out of range");
}
else
{
var decimalSeparator=".";
var val=""+x;
if(val.indexOf(decimalSeparator)<val.length-3)
{
//alert("too much decimal");
}
}
}
方式2:
使用正则表达式
WAY 2:
using Regular Expressions
function ValidateValue(str)
{
var regexPattern = /(^100(\.0{1,2})?
)|(^([1-9]([0-9 ])?| 0)(\.[0-9] { 1 , 2 })?
)|(^([1-9]([0-9])?|0)(\.[0-9]{1,2})?
)/i; 如果(!str.test(regexPattern)) { alert(" ); } }
)/i; if(!str.test(regexPattern)) { alert("value out of range or too much decimal"); } }
这篇关于文本框应验证百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文