integer


I

ichor

hi how do i validate that the user always enters a string?
do i use regularexpressionvalidator or is there any easier way?
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

ichor,

Isn't any sequence of any character technically a string? What kind of
content are you looking for?
 
M

Morten Wennevik

Hi ichor,

I think there are better ways, but this should work:

bool CheckString(string s)
{
foreach(char c in s)
{
if(!Char.IsLetter(c))
return false;
}
return true;
}

Happy coding!
Morten
 
N

Nicholas Paldino [.NET/C# MVP]

ichor,

If you are looking to check if the text is a number, then you can call
the static TryParse method on the Double structure. It will attempt to
parse the string into a double, indicating whether it was successful or not.

Also, you could set a reference to Microsoft.VisualBasic and call the
IsNumeric method on the Information class in the Microsoft.VisualBasic
namespace.

Hope this helps.
 
I

ichor

sorry,

i meant

Nicholas Paldino said:
ichor,

Isn't any sequence of any character technically a string? What kind of
content are you looking for?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ichor said:
hi how do i validate that the user always enters a string?
do i use regularexpressionvalidator or is there any easier way?
 
M

Morten Wennevik

In that case you can use my code and exchange IsLetter with IsDigit :)
 
Ad

Advertisements

J

John Baro

You can create a numeric textbox by inheriting from textbox or other methods
or
You can validate at a later date when leaving the textbox or saving or
whatever like this:
Watch for spelling and case errors

double Result;
if(!double.TryParse(textbox.Text,
System.Globalization.NumberStyles.Currency,
System.Globalization.NumberFormatInfo.InvariantInfo, out Result) ||
Result < int.MinValue || Result > int.MaxValue)
{
//do whatever
}

This will try to parse the text into a double and if successfull check if it
is out of the bounds of an int.

HTH
JB
 
B

Branimir Giurov

Hi Ichor -

for a web-based application, you can use RegularExpressionValidator class.
for a windows-forms app, you can either inherit from the textbox class, or
just capture keypress and keydown events. Once you do that, you can check
which key was pressed - and eventualy cancel the input.
For visualization of the error, you can user the ErrorProvider control.

I'm pasting here an example, that checks for a valid decimal input - one I
used in the project I'm currently working on. Feel free to modify the code
and reuse it:

System.Windows.Forms.ErrorProvider err;
bool maxCredit_Valid = false;

private void txtMaxCredit_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
maxCredit_Valid = false;

if ( (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) &&
(e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9 ) &&
( e.KeyCode != Keys.Back ) )
maxCredit_Valid = false;
else
maxCredit_Valid = true;
}

private void txtMaxCredit_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
string decimalSeparator =
System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDe
cimalSeparator;
if ( ((char)e.KeyChar) == decimalSeparator[0] )
{
e.Handled = false;
return;
}

e.Handled = !maxCredit_Valid;
}

Good Luck,
Branimir
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Check if integer 1
The DataReader and integers 2
String to Integer 3
Integer to Hex 5
Synchsafe Integer 1
Integer Resources 6
Integer values 1
Ascii to integer 5

Top