Javascript question...


1

116

I have this script to maintain a 4 character entry done with a hidden field,
but i have an issue with people putting leading zeros. How can I have an
alert when a leading zero is entered and exit the script?

<script>
function sortdashno()
{
with (document.forms[0])
{
{
if (DASHNO.value.substr(0,1) = 0)
alert("Please remove leading ZERO");
else if (DASHNO.value < 10)
DASHNOSORT.value = "000" +DASHNO.value;
else if (DASHNO.value > 10 && DASHNO.value < 100)
DASHNOSORT.value = "00" +DASHNO.value;
else if (DASHNO.value > 100 && DASHNO.value < 999)
DASHNOSORT.value = "0" +DASHNO.value;
}
}
}
</script>

Thanks
David
 
Ad

Advertisements

J

Jon Spivey

If the user enters leading zeros would it not be easier to remove them in
your script rather than ask him to type again? Also to pad a number with
zeros you don't need all that elseif stuff

with(document.forms[0]){
var x = DASHNO.value, y = 0;
while (x.substr(0, 1) == '0') { x = x.replace('0', ''); y++; }
DASHNOSORT.value = '0000'.substr(0, 4 - x.length) + x;
}

Does that do what you need?
 

Similar threads

1
Replies
2
Views
994
116
1
1
Replies
4
Views
345
Jon Spivey
J
A
Replies
0
Views
135
Al Ponds
A
A
Replies
1
Views
480
Hot-text
H
B
Replies
2
Views
145
banker123
B
L
Replies
3
Views
216
Dirk Goldgar
D
R
Replies
0
Views
523
RV16
R
Top