Click to See Complete Forum and Search --> : Input limitation textbox
James B
12-05-2002, 02:48 PM
Hi,
in my textboxes, I want to prevent users from entering values higher or lower than what I want (for example: minimum 1800, maximum 4999)
At the same time, I want to check that no other characters but numbers are entered.
When either one of these conditions is not met, I want to display an alert with the appropriate text.
Any suggestions?
Thanks,
James
kapilsharma
04-27-2007, 02:29 AM
function check(strValue, intFdigit, intBdigit)
{
var strUncheck = strValue;
var intCount;
if (intFdigit < 0 || intBdigit < 0) return(1);
else if (strUncheck == null || strUncheck == "")
{
if (intFdigit == 0 && intBdigit == 0) return(0); else return(1);
}
// 1: Characters allow are '0' .. '9', ',', and '.' but not include '..', ',,', '.,', //and ',.'
var rgeFormat = /^[0-9,.]{1,256}$/;
var arsNotAllowed = Array(",.", ".,", ",,", "..");
if (!rgeFormat.test(strUncheck) ||
strUncheck.indexOf(arsNotAllowed[0]) != -1 ||
strUncheck.indexOf(arsNotAllowed[1]) != -1 ||
strUncheck.indexOf(arsNotAllowed[2]) != -1 ||
strUncheck.indexOf(arsNotAllowed[3]) != -1
)
return(1);
// 2: Split into parts
var arsParts = strUncheck.split(".");
// 3: Backword part not allow any '.' or ','
if (arsParts.length > 2 ||
(arsParts.length == 2 && arsParts[1].indexOf(",") != -1)
)
return(1);
var arsParts = strUncheck.split(".");
// 5: Final result
if (arsParts[0].length > intFdigit) return(2);
else if (arsParts.length == 2 && arsParts[1].length > intBdigit) return(3)
else return(0);
}
This function will check a value fro example 243.78
pass something like check( 243.78 , 3, 2) on keyup event
use window.event.srcelement to pick value
kapilsharma
04-27-2007, 02:30 AM
this function will return 0 for success
s.b37
04-27-2007, 04:04 AM
sorry, i just had to addy my regexp way:
var max = 4999;
var min = 1899;
var digitCheck = /^\d*$/;
// returns false if invalid
function check(num){
if (!(num>=min && num<=max && digitCheck.test(num))) return false;
return true;
}
s.b37
04-27-2007, 04:07 AM
sorry, i just had to addy my regexp way:
var max = 4999;
var min = 1899;
var digitCheck = /^\d*$/;
// returns false if invalid
function check(num){
if (!(num>=min && num<=max && digitCheck.test(num))) return false;
return true;
}
txtField_id.onBlur = function(){
var val=txtField_id.value;
if (!check(val)){
alert("Invalid number - needs to be a digit between "+min+" & "+max");
txtField_id.focus();
txtField_id.select();
return false;
}
// do stuff or nothing if you want
}
EDIT: soz for the double post