0

In my RadGrid I am using Filter for DATE Column. Date format is like this 16/12/1990. Filter textbox should allow only Numbers and /. How to write JavaScript function to do this?

function CharacterCheckDate(text, e)
{
    var regx, flg;
    regx = /[^0-9/'' ]/
    flg = regx.test(text.value);
    if (flg)
    {
        var val = text.value;
        val = val.substr(0, (val.length) - 1)
        text.value = val;
    }
} 
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Srinivas
  • 329
  • 3
  • 12
  • 32

1 Answers1

4

You don't have to worry about / in character class, (thanks Robin for pointing that out). For example,

console.log(/[^\d/]/.test("/"));
# false
console.log(/[^\d/]/.test("a"));
# true

If you are really in doubt, simply escape it with backslash, like this

regx = /[^0-9\/'' ]/

Also, you don't need to specify ' twice, once is enough.

regx = /[^0-9\/' ]/

Instead of using numbers explicitly, you can use \d character class, like this

regx = /[^\d\/' ]/

So, you could have written your RegEx, like this

regx = /[^\d/' ]/
Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • It shouldn't be necessary to escape it inside a character class, see http://stackoverflow.com/questions/9226731/escape-forward-slash-in-jscript-regexp-range – Robin Apr 29 '14 at 07:51
  • @Robin Thanks man :) Included an example in the answer now. Please check. – thefourtheye Apr 29 '14 at 07:59
  • You're welcome :) I agree with your answer but now I don't see how the regex is different from the one OP used (except cleaner). I'm not familiar with telerik-grid but OP's issue (if he has one?) might be in the rest of the code. – Robin Apr 29 '14 at 08:11