locked
Format UK telephone number RRS feed

  • Question

  • Hi All;

    Does any one please help me to format all UK telephone number

    Anyhelp much appreciated

    Thanks


    Pradnya07

    Tuesday, February 11, 2014 10:42 AM

Answers

  • Hi,

    For each of the formats you should use a distinct function, or you could combine all of them in a function (which could be hard in maintenance). Have a look at below piece of code:

    /* Pass first arguement as the name of the field you want to validate.
    The second arguement is the pattern. Use symbols as they are. Instead of numbers use #.
    For example +21-83192720. Tha pattern should be +##-######## */
    
    function PhoneNumberValidator() {
        var CurrentNumber = new String(Xrm.Page.getAttribute(arguments[0]).getValue());
        var Pattern = new String(arguments[1]);
        if (CurrentNumber.length != 0 ) {
            if (Pattern.length != CurrentNumber.length) {
                alert('Format Error. The number must be ' + Pattern);
                Xrm.Page.getAttribute(arguments[0]).setValue();
                return;
            }
            for (var i = 0; i < Pattern.length; i++) {
                if (Pattern.substr(i, 1) != '#' && CurrentNumber.substr(i, 1) != Pattern.substr(i, 1)) {
                    alert('Format Error. The number must be ' + Pattern);
                    Xrm.Page.getAttribute(arguments[0]).setValue();
                    return;
                }
                if (Pattern.substr(i, 1) == '#' && isNaN(Number(CurrentNumber.substr(i, 1)))) {
                    alert('Format Error. The number must be ' + Pattern);
                    Xrm.Page.getAttribute(arguments[0]).setValue();
                    return;
                }
            }
        }
    }


    Iranian Dynamics CRM Association | انجمن متخصصان ایرانی

    Tuesday, February 11, 2014 12:55 PM
    Moderator
  • Formatting requires validation.  First you must validate that the number is a number that can be formatted.  After that, you are going to have to make some kind of logic tree that goes through all the options for formatting a UK number.  That isn't easy, and I don't think you're going to find a comprehensive solution just posted on the forum. 

    But validation is a strong first step, I would take the validation code posted above and start working from there.


    The postings on this site are solely my own and do not represent or constitute Hitachi Solutions' positions, views, strategies or opinions.

    Tuesday, February 11, 2014 1:40 PM

All replies

  • Hi there,

    Go through the following link. use the javascript code on change of the field.

    http://www.braemoor.co.uk/software/telnumbers.shtml

    Thanks


    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT

    Tuesday, February 11, 2014 10:47 AM
  • thanks for your reply

    the code is to validate UK telephone i need to format UK telephone numbers

    http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Formatting_GB_telephone_numbers


    Pradnya07

    Tuesday, February 11, 2014 10:50 AM
  • Tuesday, February 11, 2014 11:13 AM
  • UK format is in 7 digit NSNs

    9 digit NSNs

    10 digit NSNs

    I need to code format for all of this


    Pradnya07

    Tuesday, February 11, 2014 11:49 AM
  • Hi,

    For each of the formats you should use a distinct function, or you could combine all of them in a function (which could be hard in maintenance). Have a look at below piece of code:

    /* Pass first arguement as the name of the field you want to validate.
    The second arguement is the pattern. Use symbols as they are. Instead of numbers use #.
    For example +21-83192720. Tha pattern should be +##-######## */
    
    function PhoneNumberValidator() {
        var CurrentNumber = new String(Xrm.Page.getAttribute(arguments[0]).getValue());
        var Pattern = new String(arguments[1]);
        if (CurrentNumber.length != 0 ) {
            if (Pattern.length != CurrentNumber.length) {
                alert('Format Error. The number must be ' + Pattern);
                Xrm.Page.getAttribute(arguments[0]).setValue();
                return;
            }
            for (var i = 0; i < Pattern.length; i++) {
                if (Pattern.substr(i, 1) != '#' && CurrentNumber.substr(i, 1) != Pattern.substr(i, 1)) {
                    alert('Format Error. The number must be ' + Pattern);
                    Xrm.Page.getAttribute(arguments[0]).setValue();
                    return;
                }
                if (Pattern.substr(i, 1) == '#' && isNaN(Number(CurrentNumber.substr(i, 1)))) {
                    alert('Format Error. The number must be ' + Pattern);
                    Xrm.Page.getAttribute(arguments[0]).setValue();
                    return;
                }
            }
        }
    }


    Iranian Dynamics CRM Association | انجمن متخصصان ایرانی

    Tuesday, February 11, 2014 12:55 PM
    Moderator
  • Hi Thanks for yur reply

    i need a code to format not validate


    Pradnya07

    Tuesday, February 11, 2014 12:58 PM
  • Formatting requires validation.  First you must validate that the number is a number that can be formatted.  After that, you are going to have to make some kind of logic tree that goes through all the options for formatting a UK number.  That isn't easy, and I don't think you're going to find a comprehensive solution just posted on the forum. 

    But validation is a strong first step, I would take the validation code posted above and start working from there.


    The postings on this site are solely my own and do not represent or constitute Hitachi Solutions' positions, views, strategies or opinions.

    Tuesday, February 11, 2014 1:40 PM