script to find a character in text field and see if it is not repeated

Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

I have text field  say TEXT1  which can type anything from numbers to strings to special character.

My requirement is  what if i want some specific character to be input only once.

for example if want that user can use  the character "a" (not case sensitive) only once.

i used the following script but didnot work

if(!this.getField("TEXT1").contains("a")){

this.getField("TEXT1").value = this.getField("TEXT1").valueAsString + "a";

}

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change the first line to:

if(/a/.test(this.getField("Text18").valueAsString) == false){

TOPICS
Acrobat SDK and JavaScript, Windows

Views

274

Likes

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

script to find a character in text field and see if it is not repeated

Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

I have text field  say TEXT1  which can type anything from numbers to strings to special character.

My requirement is  what if i want some specific character to be input only once.

for example if want that user can use  the character "a" (not case sensitive) only once.

i used the following script but didnot work

if(!this.getField("TEXT1").contains("a")){

this.getField("TEXT1").value = this.getField("TEXT1").valueAsString + "a";

}

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change the first line to:

if(/a/.test(this.getField("Text18").valueAsString) == false){

TOPICS
Acrobat SDK and JavaScript, Windows

Views

275

Likes

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
Mar 13, 2018 0
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

How do you want it to work, exactly? Do you want to block the user from typing a character that already appears in the field?

Or do you want to reject the value when then leave it? Something else?

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

i want that specific character doesnot appear twice in that text field.. in above example say  "a"   although they can write anything except for "a" (not more than once).... the value already entered donot have to discarded.... .. like i wrote  " I have  a dog.."    since it contains two "a"  .....   it should write something like this..... .

"I have dog"  i.e when the users presses 'a" again it should not work ... although other keys should work and they can continue without discarding the previous value .

Likes

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
Reply
Loading...
Mar 13, 2018 0
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

Use this code as the field's custom Keystroke event:

if (event.change) event.rc = event.value.indexOf(event.change)==-1;

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

this has stopped entering any character twice .. i want only specific characters  .... like  in my case .. only "a" should not be entered more than once.. .all other characters can be entered as many times as they want

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

i want to see if "a" is already there or not... if "a" is already there then we cannot enter "a"  but if "a" is not there we can enter "a" but only once... and all other character, number and special characters can be entered as many times as user wants

Likes

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
Reply
Loading...
Mar 13, 2018 0
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

Ah, that was not clear from your original question... In that case, use this code:

var allowedOnceChars = ["a"];

if (event.change && allowedOnceChars.indexOf(event.change)!=-1) event.rc = event.value.indexOf(event.change)==-1;

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

this works fine till i enter the text from keyboard... but when i try to enter "a" by making button   i can write as many as "a" i want

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

I am trying to make a virtual keyboard through buttons.... in which i have to put restriction on using the character "a" more than once........ the code you gave works fine when i enter via keyboard.. but when i use the virtual keyboard i made in form using buttons... ."a" can be typed any number of times.

Likes

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
Reply
Loading...
Mar 13, 2018 0
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

You keep omitting crucial information about what you're trying to achieve... If you're using buttons then you can easily look at the current value of the field and see if it already contains the character that button should add, and stop it from doing so in case it's already there.

I've provided you with the basic tools of how to do it. If you want me to write your full code for you you can contact me privately (try6767 at gmail.com) to discuss it further, including the price.

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

if(["a"].test(this.getField("Text18").value) == false){

this.getField("Text18").value = this.getField("Text18").valueAsString + "a";

}

i modified your script to something above but still not working

Likes

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
Reply
Loading...
Mar 13, 2018 0
Most Valuable Participant ,
Mar 13, 2018

Copy link to clipboard

Copied

Change the first line to:

if(/a/.test(this.getField("Text18").valueAsString) == false){

Likes

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
Reply
Loading...
Mar 13, 2018 0
Contributor ,
Mar 13, 2018

Copy link to clipboard

Copied

awesome it works

thanks for the response........

Likes

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
Reply
Loading...
Mar 13, 2018 0