Expand my Community achievements bar.

SOLVED

No spaces allowed in entry

Avatar

Level 9

I have a textfield that the user types in a part number. I want to prevent them from having a space in any part of the part number they enter. If they enter a space anywhere in the part number, an error message appears telling them to use an underscore (_) instead of a space.

How do I script this in JavaScript?

1 Accepted Solution

Avatar

Correct answer by
Level 8

Put this in the change event of the field:

if (xfa.event.change==" "){

xfa.host.messageBox("No spaces allowed");

xfa.event.change="";

}

or even try this:

if (xfa.event.change==" "){

xfa.event.change="_";

}

Kyle

View solution in original post

7 Replies

Avatar

Correct answer by
Level 8

Put this in the change event of the field:

if (xfa.event.change==" "){

xfa.host.messageBox("No spaces allowed");

xfa.event.change="";

}

or even try this:

if (xfa.event.change==" "){

xfa.event.change="_";

}

Kyle

Avatar

Level 9

That works great! Thanks so much Kyle.

-Don

Avatar

Level 5

This script allowed each number (e.g. 0,1, 2..) and each character (e.g. a,b, c, A, B, ..) and each special character (e.g. ; : _ § % &).

Not allowed is a space.

if(xfa.event.newText.match(/[/\s]/)) 

{

xfa.event.change = "";

}

I hope it was also helpull for you,

Mandy

Avatar

Level 9

Yes, thank you. The script Kyle provided also allows special characters ( _ , –  , (), etc... but not a space.

Avatar

Level 5

You are right. I saw and tried only the second version and thought you need something without _

Kind regards Mandy

Avatar

Level 9

Thanks Mandy. I appreciated your input and reply!

Thanks,

-Don