473,378 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Checking For Invalid Characters

Is there anything built into .NET Framework I could use
to check if a string only has numerics and alphanumerics
in it? If anything else but a numeric or alpha has been
entered I want it to error.

Thanks.
Nov 15 '05 #1
4 11616
Darin,

You can cycle through all of the characters in the string, and then
check to see if the character is valid. You can do something like this:

// Assuming pchrChar is the character.
bool pblnAlphaNumeric = ('A' <= pchrChar && pchrChar <= 'Z') || ('a' <=
pchrChar && pchrChar <= 'z') || ('0' <= pchrChar && pchrChar <= '9');

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Darin Browne" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Is there anything built into .NET Framework I could use
to check if a string only has numerics and alphanumerics
in it? If anything else but a numeric or alpha has been
entered I want it to error.

Thanks.

Nov 15 '05 #2
You can use the Parse method of the numeric types.

float realNumData = 0;
try
{
realNumData = float.Parse(<textToParse>);
}
//The string could not be parsed into a number
catch (FormatException)
{
this.statusBar1.Text = "Invalid real number";
}
//The number is out of range of the float type
catch (OverflowException)
{
this.statusBar1.Text = "Number out of range";
}
--
Bobby C. Jones
www.AcadX.com

"Darin Browne" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Is there anything built into .NET Framework I could use
to check if a string only has numerics and alphanumerics
in it? If anything else but a numeric or alpha has been
entered I want it to error.

Thanks.

Nov 15 '05 #3
Hi,

You could also use a regex, like:
Regex.IsMatch( theString, "^[\w|\d]*$");

Please, the above regular expression was not tested , test it if you are
going to use it !!

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Darin,

You can cycle through all of the characters in the string, and then
check to see if the character is valid. You can do something like this:

// Assuming pchrChar is the character.
bool pblnAlphaNumeric = ('A' <= pchrChar && pchrChar <= 'Z') || ('a' <=
pchrChar && pchrChar <= 'z') || ('0' <= pchrChar && pchrChar <= '9');

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Darin Browne" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Is there anything built into .NET Framework I could use
to check if a string only has numerics and alphanumerics
in it? If anything else but a numeric or alpha has been
entered I want it to error.

Thanks.


Nov 15 '05 #4
// Assuming pchrChar is the character.
bool pblnAlphaNumeric = ('A' <= pchrChar && pchrChar <= 'Z') || ('a' <=
pchrChar && pchrChar <= 'z') || ('0' <= pchrChar && pchrChar <= '9');


That's pretty i18n unfriendly code. I'd use Char.IsLetterOrDigit()
instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: shank | last post by:
I'm trying to qualify email addresses. Is there any reason why the following line would not catch an email address beginning with "www." ? ElseIf Left(Session("em"),4) = "www." Then I put in a...
9
by: Safalra | last post by:
The idea here is relatively simple: a java program (I'm using JDK1.4 if that makes a difference) that loads an HTML file, removes invalid characters (or replaces them in the case of common ones...
14
by: Christopher Benson-Manica | last post by:
I'm trying to check whether a string is all digits. This part is easy: function allDigits( str ) { var foo=str.split( '' ); // better than charAt()? for( var idx=0; idx < foo.length; idx++ ) {...
2
by: mike | last post by:
I had a form like below that validated that a file was there before it would submit. <form name="attach" method="POST" action="run_this_pgm.cfm" enctype="multipart/form-data"...
11
by: Astra | last post by:
Hi All Is there anyway that before I submit a form I can check the contents of an input field to see that it only contains letters and numbers. Thanks Robbie
2
by: Xam | last post by:
Hello everybody Do you know of a javascript routine that can warn if there are any pre-defined invalid chars in the filename of an INPUT file box before it is submitted with the submit button. ...
14
by: tranky | last post by:
Hi, i'm italian...so...excuse me for my english. I've a little problem....in what manner i can check a textbox for know if it contain only character from A-Z (a-z), numbers (0-9), and underscore...
4
by: Terry Olsen | last post by:
In my NNTP program, i'm using the Message-ID's as the filename (to eliminate duplicate messages coming in from different groups). My program has been working fine for months until I received some...
5
by: emma_middlebrook | last post by:
Hi I've found the rules that cover what is a valid attribute name here: http://www.w3.org/TR/2006/REC-xml-20060816/ 2.3 Common Syntactic Constructs Is there anything already in the dotnet...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.