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

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

Engaged ,
Mar 13, 2018 Mar 13, 2018

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";

}

TOPICS
Acrobat SDK and JavaScript , Windows
980
Translate
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

correct answers 1 Correct answer

Community Expert , Mar 13, 2018 Mar 13, 2018

Change the first line to:

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

Translate
Community Expert ,
Mar 13, 2018 Mar 13, 2018

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?

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018

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 .

Translate
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 ,
Mar 13, 2018 Mar 13, 2018

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

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

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018

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

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018

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

Translate
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 ,
Mar 13, 2018 Mar 13, 2018

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;

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018

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

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018

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.

Translate
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 ,
Mar 13, 2018 Mar 13, 2018

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.

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018

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

Translate
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 ,
Mar 13, 2018 Mar 13, 2018

Change the first line to:

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

Translate
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
Engaged ,
Mar 13, 2018 Mar 13, 2018
LATEST

awesome it works

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

Translate
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