i have a textbox, i need to enter only alphabet in the starting of textbox.. no integers, no special characters.... what should i do?
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
2010-06-14 14:20:39
can u explain about match() method... how can i achieve this with match()... i dont want to use serverside validators
Ramakrishna
2010-06-15 07:06:56
I updated the answer with an example
Morten Anderson
2010-06-15 09:32:49
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
2010-06-15 09:47:37
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
2010-06-22 07:50:49
+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
2010-06-14 14:39:53
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
2010-06-15 07:02:49
Actually it's both, it will generate javascript markup, and if the client has javascript disabled, it will also check server-side.
SLC
2010-06-15 09:31:58
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
2010-06-22 07:52:49