Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want source for textbox only enter phone number and allow + into the textbox starting phone number. for ex:+65478474632
Posted
Updated 21-Nov-11 23:15pm
v2

Using a regular expression validator with the pattern ^\+?\d+$ will perform the validation you need

^\+? means that the text can contain zero or one instance of the + charcater at the start of the text only

\d+$ means that the text must then contain one or more digits before the end of the text

I would recommend that you download a free tool like Expresso to help you with your regex creation process
 
Share this answer
 
for numeric validation use regular expresion field validator

and the regular expression for this is ^\d+$

and adding + is done in coding section like
string s="+"+textbox1.text;
store this "s" into the database.


hope this is working.
 
Share this answer
 
The only really reliable way to validate if a string can be interpreted as some numeric type is to try parse it, for example:

C#
bool valid = int.TryParse(myTextBox.value);


You can also estimate validity and find out problem of parsing if you use the variant of it with exception:

C#
internal struct ValidationResult {
   internal ValidationResult(double value) { this.fValue = value; } //fException remains null
   internal ValidationResult(System.Exception exception) { this.fExcepton = exception; }
   internal bool IsValid { get { return fException == null; } }
   internal System.Exception Exception { return fException; }
   internal double Value { return fValue; }
   System.Exception fException;
   double fValue;
}

ValidationResult Validate(string value) {
   try {
      return new ValidationResult(double.Parse(value));
   } catch (Exception e) {
      return new ValidationResult(e);
   }
}


Same thing with int, etc. Using generics with primitive types is difficult, to say the least.

The solutions based on regular expressions and other ways to analyze input strings are not effective. The value can pass the check but still throw out-of-range exceptions.

—SA
 
Share this answer
 
Comments
Reiss 23-Nov-11 3:40am    
The question wasn't about converting a string to an int but requiring that only numeric characters and a possible initial + sign be entered in the field, so there is no possible out-of-range exception that can be thrown by using a regex to validate the field. The method signature for int.TryParse also requires an out parameter of type int too, so it is
<pre lang="c#">
int t = 0;
bool valid = int.TryParse(myTextBox.value, out t);
</pre>

whilst this would satisfy the rules required by the OP it would also falsely pass a negative value.

UK phone numbers couldn't be stored as an int as they start with a zero, which would be trimmed off automatically, and internationally they are too big to be stored as an int anyway e.g. +441234123456
JavaScript
<script language = "Javascript">

var digits = "0123456789";

var phoneNumberDelimiters = "()- ";

var validWorldPhoneChars = phoneNumberDelimiters + "+";

var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {

        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }

    return true;
}
function trim(s)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {

        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {

        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(){
    var Phone=document.frmSample.txtPhone

    if ((Phone.value==null)||(Phone.value=="")){
        alert("Please Enter your Phone Number")
        Phone.focus()
        return false
    }
    if (checkInternationalPhone(Phone.value)==false){
        alert("Please Enter a Valid Phone Number")
        Phone.value=""
        Phone.focus()
        return false
    }
    return true
 }
</script>
 
Share this answer
 
v2
Comments
André Kraak 23-Nov-11 1:27am    
Edited answer:
Added pre tags
Use Regular expressions to validate the user input

C#
if (textBoxTelephone.Text != string.Empty && !(System.Text.RegularExpressions.Regex.IsMatch(textBoxTelephone.Text, ^(((((\d{3}))|(\d{3}-))\d{3}-\d{4})|(+?\d{2}((-| )\d{1,8}){1,5}))(( x| ext)\d{1,5}){0,1}$)
{
    // Your code here
}


Use a matching regular expression in the if condition
 
Share this answer
 
v3

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



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