// Phone number script var global_config = { formats: [ "None", "+44 (0) 123 456 7890", "(+44) 01234 456 789", "01234 567 890", "012-345-6789", "012.345.6789", "(012) 345 6789", "(012) 345-6789", "0-123-456-7890", "0.123.456.7890", "0123 456 7890", "01 234 567", "012 3456" ] }; var PhoneNrFormat = { /** * format the text into a specific format */ format: function (text, format) { // return base text if no format is applied if (format === "None") return text; // remove any characters and keep only digits text = text.replace(/\D/g, ''); //alert(text); if (format) { switch (format) { case "+44 (0) 123 456 7890": text = text.replace(/^(\d{2})(\d)(\d{1,3})(\d{1,3})?(\d{1,3})?(\d{1,3})?/g, "+$1 ($2) $3 $4 $5 $6"); break; case "(+44) 01234 456 789": text = text.replace(/^(\d{2})(\d{1,5})(\d{1,3})?(\d{1,3})?(\d{1,3})?/g, "(+$1) $2 $3 $4"); break; case "01234 567 890": text = text.replace(/(\d{1,5})(\d{1,3})(\d{1,3})/g, "$1 $2 $3"); //(/(\d{3})(?=\d)/g, "$1 "); break; case "012-345-6789": text = text.replace(/(\d{1,3})(\d{1,3})(\d{1,5})/g, "$1-$2-$3"); //(/(\d{3})(?=\d)/g, "$1-"); break; case "012.345.6789": text = text.replace(/(\d{1,3})(\d{1,3})(\d{1,5})/g, "$1.$2.$3"); //(/(\d{3})(?=\d)/g, "$1."); break; case "(012) 345 6789": text = text.replace(/^(\d{3})(\d{1,3})(\d{1,4})?(\d{1,4})?(\d{1,4})?/g, "($1) $2 $3 $4"); //(/^(\d{2})(\d{1,3})(\d{1,3})?(\d{1,3})?(\d{1,3})?/g, "($1) $2 $3 $4"); break; case "(012) 345-6789": text = text.replace(/^(\d{3})(\d{1,3})(\d{1,4})?(\d{1,4})?(\d{1,4})?/g, "($1) $2-$3 $4"); break; case "0-123-456-7890": text = text.replace(/([\d{1}])(\d{2})(?=\d)/g, "$1-$2"); break; case "0.123.456.7890": text = text.replace(/([\d{1}])(\d{2})(?=\d)/g, "$1.$2"); break; case "0123 456 7890": text = text.replace(/(\d{4})(\d{3})(\d{4})/g, "$1 $2 $3"); break; case "01 234 567": text = text.replace(/(\d{2})(\d{3})(\d{3})/g, "$1 $2 $3"); break; case "012 3456": text = text.replace(/(\d{3})(\d{4})/g, "$1 $2"); break; } } return text; }, /** * Apply the phone number format to the selected fields or variable */ applyFormatTo: function (name, format) { // create specific property for the selected field var createProperty = function (element, format) { if (element.hasProperty("PhoneNumberFormat")) { element.getProperty("PhoneNumberFormat").setValue(format); } else { var phoneNrFormatProp = element.createProperty("PhoneNumberFormat", 0); phoneNrFormatProp.setValue(format); } }; var field = Document.getField(name); if (field) { var text = field.getText(); if (text != "") { var f = this.format(text, format); if (f) { field.setText(f); } } createProperty(field, format); field.invalidate(); } else if (Document.hasGlobalTextVariable(name)) { // check if it is a global variable var globalVar = Document.getGlobalTextVariable(name); if (globalVar) { var value = globalVar.getValue(); if (value != "") { var f = this.format(value, format); globalVar.setValue(f); } createProperty(globalVar, format); } } else { // it is a local variable var matches = name.match(/([^)]+)\[([^)]+)\]$/); if (matches.length == 3) { var field = Document.getField(matches[1]); if (field) { var t = field.getTextVariable(matches[2]); if (t) { var value = t.getValue(); if (value == "") { var f = this.format(t.getValue(), format); if (f) { t.setValue(f); } } createProperty(t, format); field.invalidate(); } } } } }, /** * Loads the dialog which ask the user about the field and format */ loadDialog: function () { var fields = Document.getAllFields(); var fieldNames = new Array(); for (var i = 0; i < fields.length; i++) { fieldNames.push(fields[i].getName()); // try to get local variables var nNumOfLocals = fields[i].getNumberOfTextVariables(); if (nNumOfLocals > 0) { for (var j = 0; j < nNumOfLocals; j++) { var localVar = fields[i].getTextVariable(j); if (localVar) { fieldNames.push(fields[i].getName() + '[' + localVar.getName() + ']') } } } } // load all global variables var globalVarsNr = Document.getNumberOfGlobalTextVariables(); for (var i = 0; i < globalVarsNr; i++) { var globalVar = Document.getGlobalTextVariable(i); if (globalVar) { fieldNames.push(globalVar.getName()); } } if (fieldNames.length) { var fieldNameProp = new Array("Select the field:", fieldNames, fieldNames[0]); var formatProp = new Array("Select the format:", global_config.formats, global_config.formats[0]); var properties = new Array(fieldNameProp, formatProp); properties = propertyDialog(properties, "Apply phone number format"); if (properties) { // apply the format to the selected field this.applyFormatTo(properties[0][2], properties[1][2]); } } }, /** * Run throw all properties and identify each field which requires to apply the format * @returns {} */ run: function () { var fields = Document.getAllFields(); for (var i = 0; i < fields.length; i++) { var field = fields[i]; if (field.hasProperty("PhoneNumberFormat")) { var format = field.getProperty("PhoneNumberFormat").getValue(); var f = this.format(field.getText(), format); if (f) { field.setText(f); field.invalidate(); } } // try to get local variables var nNumOfLocals = field.getNumberOfTextVariables(); if (nNumOfLocals > 0) { for (var j = 0; j < nNumOfLocals; j++) { var localVar = field.getTextVariable(j); if (localVar) { if (localVar.hasProperty("PhoneNumberFormat")) { var format = localVar.getProperty("PhoneNumberFormat").getValue(); var f = this.format(localVar.getValue(), format); if (f) { localVar.setValue(f); field.invalidate(); } } } } } } // load all global variables var globalVarsNr = Document.getNumberOfGlobalTextVariables(); for (var i = 0; i < globalVarsNr; i++) { var globalVar = Document.getGlobalTextVariable(i); if (globalVar) { if (globalVar.hasProperty("PhoneNumberFormat")) { var format = globalVar.getProperty("PhoneNumberFormat").getValue(); var f = this.format(globalVar.getValue(), format); if (f) globalVar.setValue(f); } } } } } PhoneNrFormat.loadDialog(); PhoneNrFormat.run(); //Priority: 10000