Javascript question...



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?

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;




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

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