Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I defined a string data type and asked the user to enter their cell number, eg 1234567890. Now, how do I validate that input and check if and make sure the string consists of only numbers?

What I have tried:

I've just started. I have no idea what to try
Posted
Updated 20-Feb-18 2:43am
Comments
Mehdi Gholam 20-Feb-18 5:53am    
Try limiting to numbers from the UI layer.
BillWoodruff 20-Feb-18 23:32pm    
Is this C# ? WinForms ? Are you familiar with Linq ? Do you understand how to use key EventHandlers ?>

Hi you can block them to type character in the text box using javascript.


<asp:TextBox runat="server" ID="txt" onkeypress="return checkNumeric(event);" Width="50%" MaxLength="10"></asp:TextBox>


and the javascript function is
function checkNumeric(e) {//debugger;
    //alert(e.keyCode);
    if (window.event) // IE 
    {
        if (e.keyCode < 48 || e.keyCode > 57) {
            event.returnValue = false;
            return false;
        }
    }
    else {
        if (e.which < 48 || e.which > 57) {
            return false;
        }
    }


}
 
Share this answer
 
The long answer is that you don't. Someone might want to space their number out like

0700 123 456

They might need to add a country code

+44(0)700 123 456

They might want to add an extension

+44(0)700 123 456 x789

If I was trying to enter my number into your site your restriction would stop me.

The short answer is to use regular expressions

c# - Regex for numbers only - Stack Overflow[^]

However I'd advise you look for a regular expression that is geared toward phone numbers rather than only digits.
 
Share this answer
 
Depending on the type of number it is, you could use xxx.TryParse() (where "xxx" is the numeric data type in question).

C#
string x = "1234";
string y = "-1234";
string z = "12b34";
int value;
if (int.TryParse(x, out value))
{
    // x is indeed a valid integer, so value will be equal to 1234.
}
if (int.TryParse(y, out value))
{
    // y is a valid integer value, so value will be equal to -1234.
}
if (int.TryParse(z, out value))
{
}
else
{
     // z is NOT a valid integer.
}


When TryParse() returns, value contains the 32-bit signed integer value equivalent of the number contained in the source string, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the source string parameter is null or String.Empty, is not of the correct format, or represents a number less than int.MinValue or greater than int.MaxValue. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

All of the intrinsic numeric types have a TryParse() method.
 
Share this answer
 
v5
Comments
F-ES Sitecore 20-Feb-18 9:05am    
Many phone numbers are too big to store as int.
#realJSOP 20-Feb-18 13:03pm    
MVC has attributes that you can use to indicate that the field is a phone number, and validation occurs accordingly. If you want to validate a phone number with TryParse, use double.TryParse or decimal.TryParse. Those are big enough to handle phone numbers. I used int.TryParse as an example, and then stated that all of the intrinsic numeric types have a TryParse method. I'm covered.
F-ES Sitecore 20-Feb-18 15:46pm    
I'm pretty sure MVC doesn't have phone number validation, you might be thinking about html5 input attributes, but anything done on the client has to be done on the server too as anything on the client can be bypassed.

Next up if the OP takes your advice and has numbers too big do you really think he is going to realise he needs to use a bigger type? Or is he just going to come back and post another question; "I'm storing phone numbers in an int and I'm getting an arithmetic overflow?"

As for using double to store phone numbers, that's even worse than int as doubles are stored as approximations of numbers, they're not exact and shouldn't be used to store things were precision is vital. You'll store a phone number of 123456789 but get 12345692 when you read it back.

Data should always be stored in appropriate types and the further the type is from the data you are storing the more of a problem it is going to be. Look at the multitude of problems on here because people are storing dates as strings, or lists of ids as a comma separated string, then down the line they end up with insurmountable problems, and that's what you're going to get if you store phone numbers as any numeric data type. The minimum I'd consider storing a phone number as would be a string, but purists would probably say you should create your own PhoneNumber class to store it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900