Restricting user to entering negative and special characters into field

Sumanth16
Mega 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
Mega 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);

}
}

View solution in original post

5 REPLIES 5

I agree.  I haven't tested this, but you might also be able to move a variable outside of the 'onChange' script to make it global and set it each time a change takes place so you could track the last valid value.

var lastValidFieldValue = '';
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', lastValidFieldValue);

    }
    else {
        lastValidFieldValue = newValue;
    }
}