views:

96

answers:

4

i have a textbox, i need to enter only alphabet in the starting of textbox.. no integers, no special characters.... what should i do?

+1  A: 

You can use the TextBox.Validated event and match the content with RegEx.
Take a look here.

Itay
A: 

Using javascripts match() method and the following RegularExpression should do the trick

^[a-zA-Z].*

Here is a short example of how to implement the match method with the regex above

<script type="text/javascript">
  function validate()
  {
      var textBoxToValidate = document.getElementById('Text1');
      if(textBoxToValidate.value.match('^[a-zA-Z].*'))
      {
          alert('Is valid');
      }  
      else
      {
          alert('Is invalid');
      }
  }
</script>

<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onclick="validate()" />
Morten Anderson
can u explain about match() method... how can i achieve this with match()... i dont want to use serverside validators
Ramakrishna
I updated the answer with an example
Morten Anderson
For good measure I just wanted to point out that the the RegularExpressionValidator has clientside validation as well. I used the match method because of the javascript tag.
Morten Anderson
in the scriptfunction fnalpha(){if(event.keycode>=65 else event.returnValue=false;in the source of textbox, call this method onkeypress="return fnalpha()";i tried like this... worked properly...
Ramakrishna
+3  A: 

If you are doing this client side, you could use an asp:RegularExpressionValidator control in the following manner.

<asp:TextBox ID="inputBox" runat="server" /><br />
<asp:RegularExpressionValidator 
    runat="server" 
    ControlToValidate="inputBox" 
    ValidationExpression="^[a-zA-Z].*" 
    ErrorMessage="Input must start with a letter" />

Server side, you could simply check the first character by using char.IsLetter.

bool isValid = char.IsLetter(inputBox.Text[0]);
Anthony Pegram
regularexpression validator is at server side, but i am using javascript validations only... then how to get that value from textbox which is in asp.net form
Ramakrishna
Actually it's both, it will generate javascript markup, and if the client has javascript disabled, it will also check server-side.
SLC
A: 

In the script function fnalpha() { if(event.keycode>=65 && event.keycode<=90 || event.keycode>=97&&event.keycode<=122) event.retrunValue=true; else event.returnValue=false; }

in the source of textbox, call this method onkeypress="return fnalpha()";

i tried like this... worked properly...

Ramakrishna