PDA

View Full Version : restricting value in textbox


learner
07-19-2002, 03:32 AM
Hi,



I have to restrict the user from entering values in the textbox

like 010,020,030 and so on till 090.Similarly 001,002 and so on till 009.He can only enter values from 0-9 in the textbox(ie 0,1,2,3...9).I have restricted other characters, and special characters from entering but how to restrict him rom entering these values 010,020,030 and so on till 090.Similarly 001,002 and so on till 009.Only these set of values i should not allow him to enter.If he enters i have to validate and give him an alert message.


Thanks

learner

Benahimvp
07-19-2002, 03:54 AM
I have a solution that should work, although it's untested. Let's say the text the person is entering ends up being in textbox:
var values = textbox.split(",")
//split the text in textbox by the commas into an array

//Let's say values now has (001,2,3,4,5,006,07,8,9,010,11)

var error = "no";
for (var i = 0; i < values.length(); i++) {
if (values[i].charAt(0) == "0") {
error = "yes";
}
// If the first character of each number is a 0, then they've
// commited an error.
}

if (error == "yes") {
alert("You bad, bad, person");
}
else {
// Go on with script
}
How about that? Will that work for ya?

learner
07-19-2002, 03:39 PM
Hi,

I think my question is not c
lear,


user can enter 0 as first character but he cannot enter 01,02 till 09 as values.Similarly he cannot enter 001,002 etc.


i.e when first character is zero it cannot be followed by characters such as 1,2,3,4 till9.Similarly when first two characters are 00 then they cannot be followed by 1,2,3,4,5,6 till 9 .


The text box length is only 3 characters.I hope it is clear


Thanks

tamienne
07-19-2002, 07:20 PM
function checkValue() {
if (document.forms[0].checkVal.length != 3) error = true;
error = false;
re = /^0[1-9]0/;
error = error || re.test(document.forms[0].checkVal.value);
re = /^00[1-9]/;
error = error || re.test(document.forms[0].checkVal.value);
if (error) {
alert ("not a valid number");
}
}

Benahimvp
07-21-2002, 03:29 AM
Maybe my script was too advanced for you or something becase that's what it did. I did forget one thing though, if the person just enters zero. Change the for loop to this:
for (var i = 0; i < values.length(); i++) {
if (values[i].charAt(0) == "0") {
error = "yes";
}
if (values[i] == 0) {
error = "no";
}
// If the first character of each number is a 0, then they've
// commited an error. But if it's 0, it'll be changed back to no error.
}

learner
07-21-2002, 12:35 PM
Hi Benahimvp


The user will enter only one value .I think there is no need to split it and put into an array.If there is only one value then there will be an error in the line values.length.I am getting an error Function expected.

Anway the problem is solved Thank u for ur efort.



Thanks

learner
07-21-2002, 12:40 PM
Hi tamienne,


Can u just explain the code u have given.Especiallly the line
re=/^0[1-9]0;I made a small change in it.It is working fine otherwise.

Thanks

tamienne
07-22-2002, 08:58 PM
re=/^0[1-9]0/;

re is a regular expression.
^from the beginning of the string
0 (starts with zero)
[1-9] a number between 1 and 9 inclusive
0 after that number is a zero

ie..
0#0 where # is a number between 1-9