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
 
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Script Question... 4
Checkbox Question... 2
textbox Disable on value.... 2
Dropdown events... 2
DRW Submit Button... 5
Javascript Calendar help 3
Script question... 3
Time format hh:nn drops leading zero 0

Top