Client event Form_CustomValidate based on Regular Expression

Tips submitted by PHPMaker users
Post Reply
Wizard69
User
Posts: 16

Client event Form_CustomValidate based on Regular Expression

Post by Wizard69 »

This a script for all user that want to implement a CLIENT CUSTOM VALIDATION from client events.

In this example we validate, with a REGULAR EXPRESSION, a field for SAVE PHONE NUMBER in italian format.

The field, on the table, was named ANCELL.
To use jQuery you can read value from x_ANCELL.

First of all, select from code events CLIENT SCRIPTS -> Table-Specific -> ADD or EDIT Page > Form_CustomValidate event
Then you see this script

function(fobj) { // DO NOT CHANGE THIS LINE!
	// Your custom validation code here, return false if invalid.
	return true;
}

Substitute with this

function(fobj) { // DO NOT CHANGE THIS LINE!
	// Your custom validation code here, return false if invalid.

	var phoneNumber = fobj.elements["x_ANCELL"];
	var $n_car = fobj.elements["x_ANCELL"].value.length;

	// if x_ANCELL empty, bypass the validation check
	if ($n_car <= 0)
		return true;
		
	var phoneRGEX = /^\+393[0-9][0-9](\d{6,7})$/;   // write without any quote or double quote!

	var phoneResult = phoneRGEX.test(phoneNumber.value);

	if (phoneResult != true) 
		 return this.OnError(phoneResult, "Phone number not valid!");   // Return false if invalid
	
	return true;
}

To guide user in insert valid Phone number, I've inserted from Code events ...
SERVER EVENTS -> Talbe-Specific -> ADD or EDIT PAGE -> Page_Render event , this script to insert, under the field, an help comment

``$this->ANCELL->CustomMsg .= "<span class='help-block'><b><font color='black'>Valid phone format +393XY1234567</font><b></span>";``

I hope this can help all users that want to validate information text by a CLIENT CUSTOM VALIDATION.


Post Reply