Skip to main content
Participant
July 8, 2019
Question

How to require a text field to contain a number?

  • July 8, 2019
  • 1 reply
  • 503 views

I have a form that is filled out by users to set up their web accounts.

There is a section on the form that asks for their preferred username. The username must be between 8-18 characters, contain one number and cannot contain special characters.

For the 8-18 characters requirement, I put an 18 character limit under the Options tab and also entered the below Format Script:

var minLength = 8; // the minimum number of digits the user should enter

var maxLength = 18; // the maximum number of digits the user should enter

event.rc = true;

if (event.value && (event.value.length<minLength || event.value.length>maxLength)) {

    app.alert("Please enter between " + minLength + " and " + maxLength  + " characters.");

    event.rc = false;

}

To prevent special characters from being entered, I entered the below Keystroke Script:

event.change = event.change.replace(/[^0-9a-zA-Z]/gim,"") ;

I am wondering if anyone has any suggestions on what Format/Validation Script I can use to give an error message if the username doesn't contain a number?

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
July 8, 2019

As the custom validation script enter this:

if (event.value && /\d/.test(event.value)==false) {

    app.alert("You must enter at least one number.");

    event.rc = false;

}

Participant
July 8, 2019

Thank you so much!