0

Actually i am trying to add Validation to textbox field in ASP.NET.The validation is like first 2 characters must be Alphabets and last two characters Numeric in a max length of 4..Here is the code block

 function IsValidate(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    var Con = String.fromCharCode(charCode);
    var exp= new RegExp("[A-Za-z]{2}[0-9]{2}");
    if(exp.match(Con))
    {
    return true;
    }
    else
    {
    return false;
    }
    }

<asp:TextBox ID="txttest" runat="server" MaxLength="4" onkeypress="return IsValidate(event);"> 
</asp:TextBox>

Any help will be well appreciated...

5
  • Why are you converting the keycode pressed to a char and then validating that. I don't think that contains the text in the textbox, does it? Commented Nov 18, 2012 at 12:48
  • 1
    AMember's answer seems good, but there is another problem. Your regex only looks for 2 letters followed by 2 digits somewhere inside the string (i.e. !=asdasda5XX00asdasd=! would match, because of the XX00). You should include anchors in the pattern: ^[A-Za-z]{2}[0-9]{2}$ Commented Nov 18, 2012 at 12:55
  • I modified the pattern,not working out either m.buettner! Commented Nov 18, 2012 at 13:18
  • @Renji yes, because you are still validating only a single character. What I was saying was, that even if you ran this on the full string it would not work as you want it to. Commented Nov 18, 2012 at 13:19
  • @ m.buettner So what would be the apt code for this situation exactly..i am clueless :) Commented Nov 18, 2012 at 15:36

3 Answers 3

1

I am no expert in ASP.NET but a quick search revealed that you can use a RegularExpressionValidator to validate the contents of your textfield.

<asp:TextBox ID="txttest" runat="server" MaxLength="4"> 
</asp:TextBox>

<asp:RegularExpressionValidator 
  id="RegularExpressionValidator1" 
  runat="server" 
  ControlToValidate="txttest" 
  ErrorMessage="Your input must be 2 letters followed by 2 numbers" 
  ValidationExpression="^[A-Za-z]{2}[0-9]{2}$">
</asp:RegularExpressionValidator>

For more info go to MSDN here

Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to validate a single char to a regexp that expects to match 4 chars...

Move your validation to the onblur event or your form submission.

1 Comment

It is not possible to make it onkeypress/onchange event?
0

Just a few recomendations

  • set the regular expression like this "/..../"
  • define in the expression the beginning and the ending of the string
  • define the modifier

so the regex would end in something like this:

var exp = /^[A-Z]{2}[0-9]{2}$/i

I hope it helps, best regards.

6 Comments

I believe you should clarify that initialization with this syntax doesn't use the constructor at all (maybe by providing the full assignment line instead of just the regex). And why {2,2} instead of {2}? And lastly, if you use i, then you can remove either a-z or A-Z
why {2,2} instead of {2}?, to make sure the user gives 2 letters and numbers, I believe you should clarify that initialization with this syntax doesn't use the constructor at all (maybe by providing the full assignment line instead of just the regex), as I know, javascript also use this way ("var exp = /regex/") to define regex, f you use i, then you can remove either a-z or A-Z? ko ko ko my bad, don't set the modifier or if you set it don't specify the capital letters, I tested here rubular.com/r/q9b1Iz5I2l
{2,2} is absolutely equivalent to {2}. And yes JavaScript supports var exp = /regex/, but if the OP doesn't know it he might infer from your answer that he should put /regex/ inside the constructor.
i tried changing the expression and executed it..still it doesnt make any difference.. :(
@mechdeveloper also, for JavaScript-based regec questions you shouldn't use rubular, but either RegexPal or simply the JS console of your browser. In this case it doesn't matter, because the regex is sufficiently simple, but there are important differences between the regex flavors of JS and Ruby.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.