Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Phone Number Fields

Avatar

Former Community Member

Hello,

I have my phone number fields set up so that when the user enters a 10-digit phone number, they tab over and it appears as such: (999) 999-9999. I was wondering if there is a script that can be used to only allow 204 and 431 area codes.

Thank you in advance for any assistance you can give me.

NikDk

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

// form1.page1.subform1.telephone::exit - (JavaScript, client)

// validat formats include 1234567890, (123)456-7890 and (123) 456-7890

var regex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

var str = this.rawValue;

if (regex.test(str)) {

  var areaCode = "";

  regex = /^\d{10}$/;

  if (regex.test(str)) {

    // format is 1234567890

    areaCode = str.substring(0,3);

    if (areaCode == "204" || areaCode == "431") {

      str = "(" + str.substring(0,3) + ") " + str.substring(3,6) + "-" + str.substring(6,10);

      this.rawValue = str;

    }

    else {

      xfa.host.messageBox("The area code must be either 204 or 431.");

    }

  }

  else {

    // format is (123)456-7890 or (123) 456-7890

    areaCode = str.substring(1,4);

    if (areaCode == "204" || areaCode == "431") {

      this.rawValue = str;

    }

    else {

      xfa.host.messageBox("The area code must be either 204 or 431.");

    }

  }

}

else {

  xfa.host.messageBox("Please enter the telephone number in the format 1234567890, (123)456-7890 or (123) 456-7890.");

}

Steve

View solution in original post

1 Reply

Avatar

Correct answer by
Former Community Member

// form1.page1.subform1.telephone::exit - (JavaScript, client)

// validat formats include 1234567890, (123)456-7890 and (123) 456-7890

var regex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;

var str = this.rawValue;

if (regex.test(str)) {

  var areaCode = "";

  regex = /^\d{10}$/;

  if (regex.test(str)) {

    // format is 1234567890

    areaCode = str.substring(0,3);

    if (areaCode == "204" || areaCode == "431") {

      str = "(" + str.substring(0,3) + ") " + str.substring(3,6) + "-" + str.substring(6,10);

      this.rawValue = str;

    }

    else {

      xfa.host.messageBox("The area code must be either 204 or 431.");

    }

  }

  else {

    // format is (123)456-7890 or (123) 456-7890

    areaCode = str.substring(1,4);

    if (areaCode == "204" || areaCode == "431") {

      this.rawValue = str;

    }

    else {

      xfa.host.messageBox("The area code must be either 204 or 431.");

    }

  }

}

else {

  xfa.host.messageBox("Please enter the telephone number in the format 1234567890, (123)456-7890 or (123) 456-7890.");

}

Steve
The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----