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.

Phone field validation

Avatar

Level 9

Hello All ,

I have a text field which is used to enter the phone number in the format Pattern: (xxx) xxx-xxxx. So I have set the validation as phone number in display,edit,validation and data which is "text{'('999')' 999-9999}" , but problem is what ever the number I enter it shows the validation is failed.I want when a user enter phone number it will automatically set the phone number is the specified pattern. For Ex : If I enter - 1234567899 , then it should display (123)456-7899 in the text field. If the user tries to enter some text values instead of number it will make the value of the field blank . Any idea how to do it ?

Thanks.

Bibhu.

5 Replies

Avatar

Former Community Member

The script below is attached to the exit event of a Text Field with a maximum length of 14. The script enables the entry of 123 456 7890, also.

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

if (!(this.isNull)) {

  var str = this.rawValue;

  var regExp = /^\d{10}$/;

  if (regExp.test(str)) {

    this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(3,3) + "-" + str.substr(6,4);

  }

  else {

    regExp  = /^[1-9]\d{2}\s\d{3}\s\d{4}$/;

    if (regExp.test(str)) {

      this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(4,3) + "-" + str.substr(8,4);

    }

    else {

      regExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

      if (!(regExp.test(str))) {

        xfa.host.messageBox("Please enter the telephone number in the format '(999) 999-9999'.");

        this.rawValue = null;

      }

    }

  }

}

The script allows the entry of

1234567890 with the result (123) 456-7890

123 456 7890 with the result (123) 456-7890

(123)456-7890 with the result (123)456-7890

(123) 456-7890 with the result (123) 456-7890

otherwise the result is null.

Steve

Avatar

Level 9

Hello Steve,

Thanks a lot. That worked perfectly. I want to set the focus to the field if the result is null. Could I just add a set focus script to it ?

Thanks.

Bibhu.

Avatar

Former Community Member

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

if (!(this.isNull)) {

  var str = this.rawValue;

  var regExp = /^\d{10}$/;

  if (regExp.test(str)) {

    this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(3,3) + "-" + str.substr(6,4);

  }

  else {

    regExp  = /^[1-9]\d{2}\s\d{3}\s\d{4}$/;

    if (regExp.test(str)) {

      this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(4,3) + "-" + str.substr(8,4);

    }

    else {

      regExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

      if (!(regExp.test(str))) {

        xfa.host.messageBox("Please enter the telephone number in the format '(999) 999-9999'.");

        this.rawValue = null;

       xfa.host.setFocus(this);

      }

    }

  }

}

Avatar

Level 9

Hello Steve,

Thanks for the solution.

Bibhu.

Avatar

Former Community Member

Sorry to re-open an old thread, but I thought this would be a good place to post this question:

How would I include additional entries into this script to convert to the same end result? Specifically 123-456-7890 and 123.456.7890.

I've tried inserting another if statement with /^\d{3}\-\d{3}\-\d{4}$/; as the variable, but i always seem to break something else.

I'm pretty new at this and have been trying for quite a few hours.