The CreatorCon Call for Content is officially open! Get started here.

Mandatory fields are accepting spaces, without having to enter any character.

SNOW32
Giga Expert

Hi Everyone,

I hope you all are doing good. 🙂

Mandatory fields are accepting spaces, without having to enter any character. I need a script for checking single or multiple space too. I took reference from community and tried a code but it is working for a single space. If user will add multiple spaces so this code won't work. The code is given below:

Wrote an onChange Client Script for the field 

if (g_form.getValue('u_recommended_ltf') == ' '){ 

alert("Please fill Analysis Output field ");

g_form.setValue('u_recommended_ltf', '');

}
 find_real_file.png

Could you please help me out for this script. 

 Kind Regards,

Swati

1 ACCEPTED SOLUTION

Ahmmed Ali
Mega Sage
Mega Sage

var d = g_form.getValue('u_recommended_ltf');

d = d.replace(/\s/g, "");

if (d==""){ 

alert("Please fill Analysis Output field ");

g_form.setValue('u_recommended_ltf', '');

}

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

12 REPLIES 12

AbhishekGardade
Giga Sage

Hello Swati,

Add bewlo code in your script so it wont accept the spaces.

var userText = g_form.getValue('u_recommended_ltf');

userText = userText.replace(/^\s+/, '').replace(/\s+$/, '');
if (userText === '') {
// text was all whitespace
} else {
// text has real content, now free of leading/trailing whitespace
}

Explanation: 

/^\s+|\s+$/g searches for whitespace from either the beginning or the end of the string. The expression can be split into two parts, ^\s+ and \s+$ which are separated by | (OR). The first part starts from the beginning of the string (^) and includes as many whitespace characters it can (\s+). The second part does the same but in reverse and for the end using the dollar sign ($).
Note that \s matches spaces, tabs and line breaks.
The /g part at the end enables global searching, which allows multiple replacements (eg. not just the beginning, but the end of the string also
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Hi Abhishek,

Thanks for the reply and explanation!

Regarding the below line why r u using 3 times '=' ?

if (userText === '')

 

Many Thanks,

Swati

Hello Swati,

JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., === ) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. == ) converts the operands to the same type before making the comparison.

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Priya Shekar
Giga Guru

Hi Swathi,

Can you please try the below code,

var rem = g_form.getValue('u_recommended_ltf');

if (!rem){ 

alert("Please fill Analysis Output field ");

g_form.setValue('u_recommended_ltf', '');

}


If my reply has helped you,Kindly click the Helpful button.
If my reply is the answer you were looking for, Kindly click the Accepted Solution button.

Thanks,
Priya