Difference between revisions of "JavaScript - Validating Irish Phone Numbers"

From All n One's bxp software Wixi

Jump to: navigation, search
Line 1: Line 1:
 +
== Overview ==
 +
 
Validating Irish Phone numbers in bxp software (bxp) is very easy thanks to a piece of custom JavaScript developed and available through the fn_javascript_general.js library.
 
Validating Irish Phone numbers in bxp software (bxp) is very easy thanks to a piece of custom JavaScript developed and available through the fn_javascript_general.js library.
  
Line 43: Line 45:
 
*** True, will replace an invalid phone number with blank
 
*** True, will replace an invalid phone number with blank
 
*** False, will not replace an invalid phone number with blank
 
*** False, will not replace an invalid phone number with blank
 +
 +
 +
 +
== Extended application ==
 +
 +
 +
To save yourself even more work in a bxp Form and just tidy up what has been data entered by the user you could apply the following:
 +
 +
In the onChange of the field you put the following, changing strCDA_X_field_0_Y to the field you want to validate.
 +
 +
<syntaxhighlight lang="JavaScript">
 +
fn_MyCustomValidatePhone('strCDA_X_field_0_Y');
 +
</syntaxhighlight>
 +
 +
 +
In the onLoad of the Form, you build the MyCustomValidatePhone
 +
 +
<syntaxhighlight lang="JavaScript">
 +
 +
//One potential way of managing your phone number field
 +
function fn_MyCustomValidatePhone( strName ){
 +
 +
//Firstly get the object to work on which has the phone number
 +
var objField = document.getElementById(strName);
 +
 +
//Now we validate the number, but we do not do replacements
 +
var blResult = fn_General_ValidatePhone_Irish ( strName, false, false );
 +
 +
//If the number is valid
 +
if (blResult == true){
 +
 +
//Clean out all the non-number values
 +
objField.value = fn_General_RemoveAllStrings(objField.value);
 +
 +
}
 +
else {
 +
 +
// However you want to handle an error
 +
// We have an error and the user cannot continue till its fixed
 +
alert ( 'Not a valid number' ); // Error message to the user
 +
objField.focus(); // Put the cursor back in the box to fix it.
 +
 +
}
 +
}
 +
 +
</syntaxhighlight>

Revision as of 19:32, 10 September 2014

1 Overview

Validating Irish Phone numbers in bxp software (bxp) is very easy thanks to a piece of custom JavaScript developed and available through the fn_javascript_general.js library.


https://ww3.allnone.ie/library/javascript/fn_javascript_general.js


This library is available to all bxp pages.


The primary new functions made available are:

  • function fn_General_ValidatePhone_Irish ( strPhoneField, blReplaceWithClean, blReplaceWithBlank )
  • function fn_General_ValidatePhone_Explicit_Irish ( strPhoneField, blIsMobile, blReplaceWithClean )
  • function fn_General_RemoveAllStrings( strString )
  • function fn_General_MatchMobilePattern( strString )


The function fn_General_ValidatePhone_Irish when supplied with the field name as a text value 'field_name', will retrieve the value and validate it.


When you want a question within a bxp form to be validated by the Phone number function simply edit the question and copy the following line into the questions JavaScript onChange setting, once you have modified the below line to suit your form:

fn_General_ValidatePhone_Irish ( 'strCDA_X_field_Y_Z', true, false )


In the above function call you are required to replace the following strPhoneField and blReplaceWithClean with live data related to your form.


  • strPhoneField
    • Replace this with the strCDA_X_field_X_X id of the question you wish the validation to be performed on. This would usually be the question that you are updating at the moment


  • blReplaceWithClean
    • Replace this with either true or false
      • True, will replace the phone number entered into the script with a clean version if the number is valid i.e. 01429400 will be converted to 0035314294000ii.
      • False, will not perform the cleaning on the phone number entered if its valid i.e. 014294000 will remain 01429400


  • blReplaceWithBlank
    • Replace this with either true or false
      • True, will replace an invalid phone number with blank
      • False, will not replace an invalid phone number with blank


2 Extended application

To save yourself even more work in a bxp Form and just tidy up what has been data entered by the user you could apply the following:

In the onChange of the field you put the following, changing strCDA_X_field_0_Y to the field you want to validate.

fn_MyCustomValidatePhone('strCDA_X_field_0_Y');


In the onLoad of the Form, you build the MyCustomValidatePhone

//One potential way of managing your phone number field
function fn_MyCustomValidatePhone( strName ){

	//Firstly get the object to work on which has the phone number
	var objField = document.getElementById(strName);

	//Now we validate the number, but we do not do replacements
	var blResult = fn_General_ValidatePhone_Irish ( strName, false, false );
	
	//If the number is valid
	if (blResult == true){
	
		//Clean out all the non-number values
		objField.value = fn_General_RemoveAllStrings(objField.value);
	
	}
	else {

		// However you want to handle an error
		// We have an error and the user cannot continue till its fixed
		alert ( 'Not a valid number' );		// Error message to the user
		objField.focus();			// Put the cursor back in the box to fix it.

	}
}