Question:
string validation in asp.net?
bhavin shah
2007-02-20 10:07:57 UTC
can any one tell me how to make string validation in textbox in the asp.net (asp.20)
for example
in the textbox user can only enter the string, system should not allow any numerical values or special characters

please describe me full code
Four answers:
Kryzchek
2007-02-20 10:38:57 UTC
If might be easiest to just handled this on the client-side using JavaScript. You could handle the OnKeyPress event and ignore any non-alphabetic characters typed by the user.



See the following for an example:



http://www.w3schools.com/jsref/jsref_onkeypress.asp
jskud
2007-02-21 09:14:10 UTC
The easiest way to do this is to just strip out any non-alphabetical characters. This can be done using a function like this:

(written in C#)



private string GetOnlyAlpha(string str)

{

string sAlphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

System.Text.StringBuilder sbFinal = new System.Text.StringBuilder();

for (int x = 0; x < str.Length; x++)

{

if (sAlpha.IndexOf(str.Substring(x, 1).ToUpper()) > 0)

{

sbFinal.Append(str.Substring(x, 1));

}

}

}





Now when you go to get the value of that text box for processing, just use the GetOnlyAlpha function, and you will be set.



Good Luck!
2007-02-20 10:17:35 UTC
Take a look at compare validators or regular expression validators.



RJ
Daniel-san
2007-02-20 10:10:11 UTC
$%% pnet by ascii 23


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...