By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,818 Members | 2,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,818 IT Pros & Developers. It's quick & easy.

Validating string format in javascript

P: 7
Hi, anyone is willing to help me to do JavaScript validation for a textbox so that we can only enter correct values in the textbox which is [Year 1 in 4 digits]/[Year 2 in 4 digits] and Year 2 must be greater than Year 1. Example is like 2008/2009 while 2009/2008 is not valid. Any help would be appreciated. Thanks
Jul 8 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
You could use a regular expression for the first part e.g. /^\d{4}\/\d{4}$/

For checking whether one is greater than the other, split the string and use parseInt to convert to integers and use < or > to compare.
Jul 8 '08 #2

P: 7
You could use a regular expression for the first part e.g. /^\d{4}\/\d{4}$/

For checking whether one is greater than the other, split the string and use parseInt to convert to integers and use < or > to compare.
ok so i add
Expand|Select|Wrap|Line Numbers
  1. var exp = /^\d{4}\/\d{4}$/;
  2.  
  3.  if (exp.test(string)){
  4.     blnAllOkay = true;
  5.     } else {
  6.     blnAllOkay = false;
  7.     }
ok then what should i do to split the string? and yeah i need to make sure that the year difference is one year. thanks.
Jul 9 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Use the split() method to split the string. If you need to make sure there's only a one year difference, compare both the numbers, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (num1==(num2+1)) {...
Jul 9 '08 #4

P: 7
Many thanks for your help. Im really grateful =D

Cheers.
Jul 9 '08 #5

acoder
Expert Mod 15k+
P: 16,027
No problem. You're welcome :)
Jul 9 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.