Restricting user to entering negative and special characters into field

Sumanth16
Kilo Patron

I am using this client script to restrict entering negative value and special characters into field,

1) When user enters negative value into field it is populating alert window ,when user clicks ok it setting field value to blank.But I need to set field value to previous value.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var specialCharRegex = /^[0-9].*$/;
if(!specialCharRegex.test(newValue)){
alert('please do not enter negative value and special characters into field');
g_form.setValue('field','');

}
}

 

Thanks in Advance

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Try this

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var specialCharRegex = /^[0-9].*$/;
if(!specialCharRegex.test(newValue)){
alert('please do not enter negative value and special characters into field');
g_form.setValue('field',oldValue);

}
}


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

SanjivMeher
Kilo Patron
Kilo Patron

Try this

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var specialCharRegex = /^[0-9].*$/;
if(!specialCharRegex.test(newValue)){
alert('please do not enter negative value and special characters into field');
g_form.setValue('field',oldValue);

}
}


Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,

 

Thanks for your response.It is not working ,the field value is setting to blank only.

FYI.  'oldValue' will always be the value set in the field when the form loads.  If the field is empty when the form loads, you would always end up with a blank field with this script.

In that case, I would also add this script to onSubmit script and not clear the field on onChange. And not allow the user to submit. User can then manually change it and wont loose what he has entered.


Please mark this response as correct or helpful if it assisted you with your question.