• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to require a text field to contain a number?

Community Beginner ,
Jul 08, 2019 Jul 08, 2019

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript

Views

322

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 08, 2019 Jul 08, 2019

Copy link to clipboard

Copied

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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 08, 2019 Jul 08, 2019

Copy link to clipboard

Copied

LATEST

Thank you so much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines