Validating phone field

Ron 17 Apr, 2012
Hello,

I have working on a java script to suit my need of validating phone numbers but I am not succeeding here is the .js code below:

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        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.myform_name.phone
	//IF the user also presses Cancel, we will approve the form submission!
	if ((Phone.value==null)||(Phone.value=="")){
		alert("A \"Customer Resource\" executive can provide you Better Support through telephone.\n \n We are a responsible company and provides service to its worldwide customers effectively. \n \n To enter your telephone number just press\"Ok\" or else press \"Cancel\". ")
		Phone.focus()
		return true;
	  } else
       {
        return false;
       }
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false;
          }
          return true;
        }


Would you please check why this is not working. I am also wanting to put up a dynamic .js file that
could validate all my forms phone field. I do not want to enter the form name rather I want this script to be called onSubmit. Example: onSubmit="return ValidateForm()"

Thanks

Ronn
GreyHead 18 Apr, 2012
Hi Ron,

Sorry, I don't have time to debug your custom JavaScript.

If you are using CFv4 you could add a Custom validation using a Regex for the phone number. See this StackOverflow post for some suggestions.

Bob
Ron 18 Apr, 2012
Hello Bob 8) ,

I do not use the Wizard to create forms. I have a custom code. I would like to use this file as a dynamic file called when the index.php page loads. so this way I do not have to load in the forms js. area and also I do not get a java script Alert popup as a reminder.

To view a sample Please Click here...

Thanks Bob. You are really working hard taking care of the Rocking forum.

Ron
GreyHead 18 Apr, 2012
Hi Ron,

You can still use the ChronoForms validation with Custom HTML - and you can include the script from a file if you like.

Bob
Ron 22 Apr, 2012

Hi Ron,

You can still use the ChronoForms validation with Custom HTML - and you can include the script from a file if you like.

Bob



Thanks for your reply Bob.

I will see how best I can use it.

Ron
This topic is locked and no more replies can be posted.