Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Numeric Fields

Avatar

Level 4

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
Level 10

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
Level 10

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

Level 4

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

Level 10

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