Forums

Partly telephone field validation php code for form field.

Ron 15 Nov, 2013
Hello

I have been trying to make a php code for telephone form field validation for international numbers. When a user lets the field empty it shows a popup display. If the user fills in the field then it has to be filled in rightly. It has errors I am not able to work it. Can someone help ?


  $answer = true;
  $submit = addEvent('click', function(event) {
          //Read the value from the form input
          var phoneNumber = $('phonebox').value;
          //IF the length of the phonenumber is 0, then we ask the user about it...
          if (phoneNumber.length == 0)
          {
             //IF the user also presses Cancel, we will approve the form submission!
             if (!confirm("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 provide us your telephone number just press\"Ok\" or else press \"Cancel\". "))
             {
                return true;
             } else
             {
                return false;
             }
          } else if (phoneNumber.length != 13)
          {
             alert("Invalid phone number length! Please try again.");
             return false;
             //Use a regular expression to see whether the phone number only contains digits...
          } else if (!phoneNumber.match(/^((\+)?[1-9]{1,2})?([\-\s\.])?((\(\d{1,4}\))|\d{1,4})(([\-\s\.])?[0-9]{1,12}){‌​1,2}(\s*(ext|x)\s*\.?:?\s*([0-9]+))?$/ –))
          {
             alert("Invalid characters in your phone number. Please try again.");
             return false;
          }
          return true;
  }
    
 


Thanks for a possible help.

Ron
Max_admin 16 Nov, 2013
Hi Ronn,

You need a custom validation function for that, did you check our FAQs page for custom validation ?

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
Ron 20 Nov, 2013
Hello Max.

Thanks for the reply.

You need a custom validation function for that, did you check our FAQs page for custom validation ?



Well I checked and I did not find any answer suitable so I posted the code. This code has an error somewhere I am not able to find. But I have another javascript that works. But partly. It does not work onSibmit and keeps giving me a popup. It also does not recognise the validity of the phonebox at the end of the code . I am posting this .js code below. I will be grateful if you have a look at it:

var defaultPhone = "";
var country = "us";


/* ************************************** */
function load() {
	/*
	
	*/
	
	$('#phonebox').val(defaultPhone);
	$('#phonebox').focus();
	
	processPhone();
}


// -------------------------------------------------------------------------   
function clickPhone() {
	/*
	
	Called when the phone box is clicked
	
	*/

	if ($('#phonebox').val() == defaultPhone) {
		$('#phonebox').val('');
	}
	
	processPhone();
}


// -------------------------------------------------------------------------   
function processPhone() {
	/*
	
	Process the phone number
	
	*/

	phone = $('#phonebox').val();
		
	if (phone.length == 0) {
		$('#popup').fadeOut('slow');
	} else {
		$('#popup').fadeIn('slow');
	}
	
	var country = $('#select_country').val();
	
	var e164 = formatE164(country, phone);
	if (e164.substring(0, 1) == "+") {
		$('#phone_e164').html(e164);
	} else {
		$('#phone_e164').html('+');
	}
	$('#phone_international').html(formatInternational(country, phone));
	$('#phone_national').html(formatLocal(country, phone));
	var countryCode = countryForE164Number(formatE164(country, phone));
	if (countryCode.length == 0) { 
		$('#phone_country').html('-');
	} else {
		$('#phone_country').html(countryCode + " - " + countryCodeToName(countryCode));
		
	/* Check the validity of the telephone number */	
		
	}
	$('#phone_mobile_dial').html(formatNumberForMobileDialing(country, phone));
	if (isValidNumber(phone)) {
		$('#phone_valid').html('Yes');
	} else {
		$('#phone_valid').html('No');
	}
       /* When the telephone number is not valid. confirm with the popup message.*/
	   
	      if ($('#phone_valid').html('No')); {
        confirm('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 provide us your telephone number just press\"Ok\" or else press \"Cancel\". ');
		return true;
		  }
      }


Please help.

Thanks

Ron
Max_admin 22 Nov, 2013
Hi Ron,

Please search the FAQs page for "custom validation", it will be the first result, however, you will need a valid regex for your phone field format, the code you provide here may work on the form but it will not look like the default form validation in a tooltip.

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
GreyHead 23 Nov, 2013
Hi Ron,

Sorry but it's really not practical for us to debug complex custom code for you, especially when it's only loosely linked to the ChronoForms code. As Max said, please check the Custom Validation FAQ and try adding your regex directly to that. But, be warned that it is tricky to get good validation of international phone numbers as there are so many variants in use around the world.

Bob
Ron 26 Nov, 2013
Thanks Bob,

I understand your position.

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