To display error msg when Requested for user email starts with 'ab' or 'AB'

rishabh31
Mega Sage

Dear Team,

 

I am working on a catalog item containing a reference type variable 'requested_for_user'. Requirement is display error message if the user selected in 'requested_for_user' field has email that starts with either 'ab' OR 'AB'.

 

I tried to achieve this through on change client script (on requested_for_user) and below script, but no luck 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var reqfremail = g_form.getReference("requested_for_user");
//if((reqfremail.substring(0,2) == "ab") ||(reqfremail.substring(0,2) == "AB")){
	if ((reqfremail.email.indexOf ("ab")>-1) || (reqfremail.email.indexOf ("AB")>-1)){
g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB'", "error");
}
//}
}

 

rishabh31_0-1700719195640.png

 

as throwing below error

rishabh31_0-1700718676088.png

 

the same error is displayed in both cases i.e., when using 'substring' OR 'indexOf'.

Even I tried to remove the Email value 'email' from the client script but still did not work.

 

Kindly help to get this work as expected.

 

Thanks in advance

 

 

 

2 ACCEPTED SOLUTIONS

Samaksh Wani
Giga Sage
Giga Sage

Hello @rishabh31 

 

As you are using getReference, you must have to use callback function for the same.

 

Replace your script with mine:-

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var reqfremail = g_form.getReference("requested_for_user", requestEmail);

function requestEmail(reqfremail){
	if ((reqfremail.email.indexOf ("ab")>-1) || (reqfremail.email.indexOf ("AB")>-1)){
g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB', Please select", 'info');
}
}

}

 

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

View solution in original post

Hi 

The below script is also working with the use of 'substring' in place of 'indexOf'

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var reqfremail = g_form.getReference("requested_for_user", requestEmail);

function requestEmail(reqfremail){
	//if ((reqfremail.email.indexOf ("ab")>-1) || (reqfremail.email.indexOf ("AB")>-1)){
if ((reqfremail.email.substring (0,2) < "ab") || (reqfremail.email.substring (0,2) < "AB")){
g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB', Please select", "error");
}
}

}

 

View solution in original post

4 REPLIES 4

Samaksh Wani
Giga Sage
Giga Sage

Hello @rishabh31 

 

As you are using getReference, you must have to use callback function for the same.

 

Replace your script with mine:-

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var reqfremail = g_form.getReference("requested_for_user", requestEmail);

function requestEmail(reqfremail){
	if ((reqfremail.email.indexOf ("ab")>-1) || (reqfremail.email.indexOf ("AB")>-1)){
g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB', Please select", 'info');
}
}

}

 

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Hi 

The below script is also working with the use of 'substring' in place of 'indexOf'

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var reqfremail = g_form.getReference("requested_for_user", requestEmail);

function requestEmail(reqfremail){
	//if ((reqfremail.email.indexOf ("ab")>-1) || (reqfremail.email.indexOf ("AB")>-1)){
if ((reqfremail.email.substring (0,2) < "ab") || (reqfremail.email.substring (0,2) < "AB")){
g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB', Please select", "error");
}
}

}

 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @rishabh31 ,

 

Try like this

 

if (reqfremail.email.startsWith ("ab") || reqfremail.email.startsWith ("AB")){

g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB'", "error");

}

 

https://www.w3schools.com/jsref/jsref_startswith.asp

 

Thanks,

Danish

 

Dibyaratnam
Tera Sage

Before writting that indexOf if conditions, modify it to below. It should resolve the issue.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var reqfremail = g_form.getReference("requested_for_user");
//if((reqfremail.substring(0,2) == "ab") ||(reqfremail.substring(0,2) == "AB")){
if(reqfremail.email){
	if ((reqfremail.email.indexOf ("ab")>-1) || (reqfremail.email.indexOf ("AB")>-1)){
g_form.showFieldMsg("requested_for_user", "Selected user email starts with 'ab' OR 'AB'", "error");
}}
//}
}