Text field needs to validate the entered value is within the specified range from the other field in the form.

Abhishek Barik
Tera Contributor

HI Team,

We have a requirement to validate a text field within the specified range from the other field in the form. Fr ex if the field "a" has a IP range of "192.168.1.0/24" , the other field "b" should be between 0-24 with a valid IP address. Ex: between 192.168.1.1 to 102.168.1.24

Please help in the script.

Thanks in advance,

Abhishek

1 ACCEPTED SOLUTION

So it's not a CIDR.

It'll be much easier.

Create a regex like below for field "a"

^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.[01]?\d\d?|\.2[0-4]\d|\.25[0-5]){3}(\/([01]?\d\d?|2[0-4]\d|25[0-5]))$

Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var mask = g_form.getValue('a').split('/');
        var end = mask[1];

		var maskOctet = mask[0].split('.');
        var endOctet = newValue.split('.');
		for (var i=0; i<3; i++) {
			if (maskOctet[i] != endOctet[i]) {
				g_form.showFieldMsg('b', 'invalid ip address.', 'error');
				return;
			}
		}
		if (endOctet[3] < 1 || endOctet[3] > end) {
			g_form.showFieldMsg('b', 'invalid ip address.', 'error');
		}
    } catch (e) {
        alert(e.message);
    }
}

View solution in original post

5 REPLIES 5

Thanks, Its working fine 🙂