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

Numeric Fields

Avatar

Former Community Member

Hello,

I have a field which the user enters a number. The field is 9 digits long, and the number that the user enters needs to start with the number 1. Is it possible to format the field to start with a 1?


Thank you,

Nik

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

My preference is use a Text Field object and script to do validation. This may not be for everyone. I defined a Text Field called 'tf' with a maximum character length of 9 and added the script below to the exit event.

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


if (!(this.isNull)) {

  var tf = this.rawValue;

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

  if (regExp.test(tf)) {

    if (tf.substr(0,1) != "1") {

      xfa.host.messageBox("The field must be start with a 1.","Validation",1);

    }

  }

  else {

    xfa.host.messageBox("The field must be 9 digits starting with a 1.","Validation",1);

  }

}

View solution in original post

3 Replies

Avatar

Correct answer by
Former Community Member

My preference is use a Text Field object and script to do validation. This may not be for everyone. I defined a Text Field called 'tf' with a maximum character length of 9 and added the script below to the exit event.

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


if (!(this.isNull)) {

  var tf = this.rawValue;

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

  if (regExp.test(tf)) {

    if (tf.substr(0,1) != "1") {

      xfa.host.messageBox("The field must be start with a 1.","Validation",1);

    }

  }

  else {

    xfa.host.messageBox("The field must be 9 digits starting with a 1.","Validation",1);

  }

}

Avatar

Former Community Member

Thank you for your help. I do have one more question. If i enter in a number that starts with somethign other than 1 it gives me the error message

but the number still shows up in the field. If i leave it there, it still lets me submit the form, even though the format was incorrect. Since the field is not mandatory can i still make it mandatory that the correct number is entered?

Avatar

Former Community Member

There are a variety of techniques for getting this done. It depends on how you want to present error messages. One easy technique is to define your submit button, make it "hidden", and add a second "regular" button which executes validation script and calls the "click" event on the hidden submit button.

// form1.page1.callSubmitBtn::click - (JavaScript, client)

if (form1.page1.tf.isNull) {

  xfa.host.messageBox("Text field is a mandatory field.");

}

else {

  form1.page1.submitBtn.executeEvent("click");

}

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] ----