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
BedrockMission!

Learn More

View all

Sign in to view all badges

Regular Expression to make Text Field only hold 0-9 is allowing non numeric

Avatar

Avatar
Validate 1
Level 2
berlink0304
Level 2

Likes

3 likes

Total Posts

5 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
berlink0304
Level 2

Likes

3 likes

Total Posts

5 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
View profile
berlink0304
Level 2

22-09-2017

Hello,

I need a field in which a user is limited to a 3 digit entry which allows the numbers 0-9.

Currently I am using a text field as my object on my form, so I can limit the length to three characters.

I have looked into regular expressions in order to do what I want, but the problem is that I still can enter ".", "-", and "-".  That's the numpad Decimal, numpad Minus, and the dash keys.  I am unsure why these are allowed, but I need them to not be, as it allows a user to enter a negative or a decimal number into the field.  I imagine something isn't quite right with my regex or how I'm applying it.

If there's a better way of going about this, I'm all ears.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 3
Level 2
Naresh_Katkam
Level 2

Likes

4 likes

Total Posts

8 posts

Correct Reply

3 solutions
Top badges earned
Boost 3
Boost 1
Affirm 3
Affirm 1
View profile

Avatar
Boost 3
Level 2
Naresh_Katkam
Level 2

Likes

4 likes

Total Posts

8 posts

Correct Reply

3 solutions
Top badges earned
Boost 3
Boost 1
Affirm 3
Affirm 1
View profile
Naresh_Katkam
Level 2

27-10-2017

Hi,

1). Using Number Field

For your requirement use XTYPE as numberfield. Add property to that node/field as maxValue.(Best Approach )

Example:

<limit jcr:primaryType="cq:Widget"
  fieldLabel="Limit"
  name="./limit"
  maxValue="9"
  xtype="numberfield"/>

2). Using Regex.

<limitjcr:primaryType="cq:Widget"
  fieldLabel="Limit"
  name="./limit"
  regex="/[0-9]{1}$/"
  regexText="Enter 0-9 values only."
  xtype="textfield"/>

Answers (5)

Answers (5)

Avatar

Avatar
Boost 3
Level 2
Naresh_Katkam
Level 2

Likes

4 likes

Total Posts

8 posts

Correct Reply

3 solutions
Top badges earned
Boost 3
Boost 1
Affirm 3
Affirm 1
View profile

Avatar
Boost 3
Level 2
Naresh_Katkam
Level 2

Likes

4 likes

Total Posts

8 posts

Correct Reply

3 solutions
Top badges earned
Boost 3
Boost 1
Affirm 3
Affirm 1
View profile
Naresh_Katkam
Level 2

29-10-2017

HI,

1). Using Regex

below example will allows 0-99 values only.

it will match your requirement.

<limit jcr:primaryType="cq:Widget"

  fieldLabel="Limit"

  name="./limit"

  regex="/^([0-9]{2}|[0-9]{1})$/"

  regexText="Enter 0-99 only.Number's only"

  xtype="textfield"/>

2). Using numberfield

you can free to use Number field as XTYPE. it works better for you.

If you give maxValue as 99. that field allow 0-99 values only.

Example:

<limit jcr:primaryType="cq:Widget"

  fieldLabel="Limit"

  name="./limit"

  maxValue="99"

  xtype="numberfield"/>

Thanks.

Avatar

Avatar
Validate 10
Level 4
mjb54261515
Level 4

Likes

33 likes

Total Posts

132 posts

Correct Reply

32 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 25
View profile

Avatar
Validate 10
Level 4
mjb54261515
Level 4

Likes

33 likes

Total Posts

132 posts

Correct Reply

32 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 25
View profile
mjb54261515
Level 4

29-10-2017

berlink0304

^(0|[1-9][0-9]*)$ this for two digit between 0 to 9 and also allow 0

ref: Regex pattern for numeric values - Stack Overflow

it think  you can try ^(0|[1-9][0-9][0-9]*)$

Avatar

Avatar
Affirm 1
Employee
DavinderKaur
Employee

Likes

0 likes

Total Posts

3 posts

Correct Reply

1 solution
Top badges earned
Affirm 1
View profile

Avatar
Affirm 1
Employee
DavinderKaur
Employee

Likes

0 likes

Total Posts

3 posts

Correct Reply

1 solution
Top badges earned
Affirm 1
View profile
DavinderKaur
Employee

30-10-2017

As mentioned above best approach is to use the numeric field. If server is 6.3 onwards then there is a property (Maximum Number of Digits) to set the maximum no. of allowed digits in numeric field that can control the limit of entered digits.

numeric_field_properties.png

Avatar

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,198 likes

Total Posts

6,369 posts

Correct Reply

1,147 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,198 likes

Total Posts

6,369 posts

Correct Reply

1,147 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile
kautuk_sahni
Community Manager

29-10-2017

TundraSteve any further thoughts?

Avatar

Avatar
Give Back 25
Level 5
TundraSteve
Level 5

Likes

51 likes

Total Posts

442 posts

Correct Reply

57 solutions
Top badges earned
Give Back 25
Give Back 10
Give Back 5
Give Back 3
Give Back
View profile

Avatar
Give Back 25
Level 5
TundraSteve
Level 5

Likes

51 likes

Total Posts

442 posts

Correct Reply

57 solutions
Top badges earned
Give Back 25
Give Back 10
Give Back 5
Give Back 3
Give Back
View profile
TundraSteve
Level 5

27-10-2017

Why are you not using a numeric field and setting the Maximum Number of Digits to 3?