- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 08:20 PM
How to accept minimum length and maximum length of a string from the user
I have written a OnChange Catalog client script for the same. This script works fine if I want to accept length 10, In this I also want to accept it if length is 4. I want it to accept both length 10 and 4, if it is not 10 or 4 then it should throw an error.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}var letterNumber = /^[0-9a-zA-Z]+$/;
if(!(newValue.match(letterNumber)) || newValue.toString().length>10 || newValue.toString().length<10)
{
alert('It accepts only 10 digits or character');
g_form.setValue('vehicle_registration_no', '');
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 09:32 PM
Please try below code:
---------------------------------------------
var regexcheck=/^[A-Za-z0-9]*$/.test(newValue);
len=newValue.toString().length;
if(regexcheck &&(len==4 || len==10 )){
//do nothing
}else{
alert("Your error msg");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 09:01 PM
Try the following code
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var letterNumber = /^[0-9a-zA-Z]+$/;
if( !(newValue.match(letterNumber)) && ( newValue.toString().length !=10 || newValue.toString().length != 4) ) //An alert is displayed if any of the following conditions fail: first, check the regex; second, either value must be in maximum or minimum length.
{
alert('It accepts only 10 digits or characters);
g_form.clearValue('vehicle_registration_no');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 09:20 PM
This is not working, it is accepting any length
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 10:02 PM - edited 10-11-2022 10:05 PM
Try this using regex
var regexcheck=/^[A-Za-z0-9]*$/.test(newValue);
var minLengthCheck = /^[a-zA-Z]{4}$/.test(newValue); //Regex to check minimum length
var mxLengthCheck = /^[a-zA-Z]{10}$/.test(newValue); //Regex to check maximum length
if(regexcheck && (minLengthCheck || mxLengthCheck )){
//do nothing
}else{
alert("error msg");
g_form.clearValue('vehicle_registration_no');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 09:05 PM
Hi Pratiksha,
See if this helps,
if(g_form.getValue('field_name').length==4 || g_form.getValue('field_name').length==10){
g_form.addInfoMessage('valid value');
} else {
g_form.addInfoMessage('invalid value');
}
Thanks,
SK
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....