Javascript Check User Input

Hi Everyone

I have attached a javascript function that is called onkeyup which should the user enter any characters that are not numbers it will replace this with an empty space.

What I would like to do now is to validate an expiry date entered in the format mm/yy.  So for example the first,second, fourth and fifth character must be a digit.  The third character must be the / character.  If the user enters anything expect this format then I want to replace what they have typed with an empty space like before.

Any help greatly appreciated thanks

function numInputOnly(ob) 
  var invalidChars = /[^0-9]/gi
  ob.value = ob.value.replace(invalidChars,"");


Open in new window

Lisa BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

test page :

function validateMMsYY(ob) {
   var v = "";
   for(var i=0;i<ob.value.length;i++) {
       if((i<2||i>2)&&ob.value.charAt(i)>=0&&ob.value.charAt(i)<=9) {
           v = v + ob.value.charAt(i);
       else if(i==2&&ob.value.charAt(2)=="/") {
          v = v + "/";
   ob.value = v;

Open in new window

If you're familiar with jQuery and plugins check this :

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lisa BAuthor Commented:

Thank you very much for the answer and the additional information I really appreciate it - works a treat
Lisa BAuthor Commented:
Great answer and further information very helpful as well thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.