Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Masking in JavaScript

Status
Not open for further replies.

rddart

Programmer
Aug 11, 2006
1
MT
Hi,

I'm developing a small application and I'm validating with JavaScript. I have a particular field that needs particular data/characters in it. This field must be in this format: NN/ZZZZ

where NN must be between 06 and 20
/ is fixed in position 3
ZZZZ must be between 0001 and 9999

Nothing else should be accepted in this field. I want that when the user inputs 8/1, the field is automatically corrected to 08/0001.

Thanks for your help.
 
Well first you'd use .split('/') to split your value up into two chunks in an array (a left side of the slash "NN" and a right side of the slash "ZZZZ").

Then you can use parseInt() on each chunk to convert them into integer values. Once they're integers then you simply use the standard comparison operators to check that the integer values fall within your acceptable range.

Then pad out both sides with your leading zeros and join them together around a slash to get your final value.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Webflo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top