Need help in validation

Friends,

I need to have validation on lead entry form or say in any entry form. Condition is :

Either email or phone need to be entered so its and/or type of validation.

Any suggestion how it can be achieved??

I would appreciate your help.

-Harsh

The and/or makes it a little more difficult, if you just needed an AND validation you could set them both to required fields. You could do a before save logic hook or you could write a workflow to check them both but neither option is perfect (or even particularly good).

I would probably go and read this, it looks like it would suit your needs.

Thank you for your suggestion, either way key is to cancel saving data if validation fails which i am not able to achieve currently.

With respect to the web-to-lead form, if created with SuiteCRM, a portion of javascript code for validation is appended at the end of the form.

You could easily add there the condition that will return false if both email AND phone are empty.

In that case you also have to change the message returned by the alert that comes immediately after.

for completion the following is the javascript:

<script type=\"text/javascript\">
// <![CDATA[
 function submit_form(){
 	if(typeof(validateCaptchaAndSubmit)!='undefined'){
 		validateCaptchaAndSubmit();
 	}else{
 		check_webtolead_fields();
 	}
 }
 function check_webtolead_fields(){
     if(document.getElementById('bool_id') != null){
        var reqs=document.getElementById('bool_id').value;
        bools = reqs.substring(0,reqs.lastIndexOf(';'));
        var bool_fields = new Array();
        var bool_fields = bools.split(';');
        nbr_fields = bool_fields.length;
        for(var i=0;i<nbr_fields;i++){
          if(document.getElementById(bool_fields[i]).value == 'on'){
             document.getElementById(bool_fields[i]).value = 1;
          }
          else{
             document.getElementById(bool_fields[i]).value = 0;
          }
        }
      }
    if(document.getElementById('req_id') != null){
        var reqs=document.getElementById('req_id').value;
        reqs = reqs.substring(0,reqs.lastIndexOf(';'));
        var req_fields = new Array();
        var req_fields = reqs.split(';');
        nbr_fields = req_fields.length;
        var req = true;
        for(var i=0;i<nbr_fields;i++){
          if(document.getElementById(req_fields[i]).value.length <=0 || document.getElementById(req_fields[i]).value==0){
           req = false;
           break;
          }
        }
        if(req){
            document.WebToLeadForm.submit();
            return true;
        }
        else{
          alert('Please provide all the required fields');
          return false;
         }
        return false
   }
   else{
    document.WebToLeadForm.submit();
   }
}
function validateEmailAdd(){
	if(document.getElementById('webtolead_email1').value.length >0) {
		if(document.getElementById('webtolead_email1').value.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == null){
		  alert('Not a valid email address');
		}
	}
	if(document.getElementById('webtolead_email2').value.length >0) {
		if(document.getElementById('webtolead_email2').value.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == null){
		  alert('Not a valid email address');
		}
	}
}
// ]]>
</script>";

Your if statement should go just before

        if(req){
            document.WebToLeadForm.submit();
            return true;
        }

and it could be something like:

        var extra_msg='';
        if( (document.getElementById('webtolead_email1').value.length =0) && (document.getElementById('phone_work').value.length =0)){
            extra_msg=' - You must enter at least one of email or phone';
            req = false;
        }

Then change the following alert statement to:

        else{
          alert('Please provide all the required fields'+extra_msg);
          return false;
         }

So the whole thing should become:

<script type=\"text/javascript\">
// <![CDATA[
 function submit_form(){
 	if(typeof(validateCaptchaAndSubmit)!='undefined'){
 		validateCaptchaAndSubmit();
 	}else{
 		check_webtolead_fields();
 	}
 }
 function check_webtolead_fields(){
     if(document.getElementById('bool_id') != null){
        var reqs=document.getElementById('bool_id').value;
        bools = reqs.substring(0,reqs.lastIndexOf(';'));
        var bool_fields = new Array();
        var bool_fields = bools.split(';');
        nbr_fields = bool_fields.length;
        for(var i=0;i<nbr_fields;i++){
          if(document.getElementById(bool_fields[i]).value == 'on'){
             document.getElementById(bool_fields[i]).value = 1;
          }
          else{
             document.getElementById(bool_fields[i]).value = 0;
          }
        }
      }
    if(document.getElementById('req_id') != null){
        var reqs=document.getElementById('req_id').value;
        reqs = reqs.substring(0,reqs.lastIndexOf(';'));
        var req_fields = new Array();
        var req_fields = reqs.split(';');
        nbr_fields = req_fields.length;
        var req = true;
        for(var i=0;i<nbr_fields;i++){
          if(document.getElementById(req_fields[i]).value.length <=0 || document.getElementById(req_fields[i]).value==0){
           req = false;
           break;
          }
        }
        var extra_msg='';
        if( (document.getElementById('webtolead_email1').value.length =0) && (document.getElementById('phone_work').value.length =0)){
            extra_msg=' - You must enter at least one of email or phone';
            req = false;
        }
        if(req){
            document.WebToLeadForm.submit();
            return true;
        }
        else{
          alert('Please provide all the required fields'+extra_msg);
          return false;
         }
        return false
   }
   else{
    document.WebToLeadForm.submit();
   }
}
function validateEmailAdd(){
	if(document.getElementById('webtolead_email1').value.length >0) {
		if(document.getElementById('webtolead_email1').value.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == null){
		  alert('Not a valid email address');
		}
	}
	if(document.getElementById('webtolead_email2').value.length >0) {
		if(document.getElementById('webtolead_email2').value.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == null){
		  alert('Not a valid email address');
		}
	}
}
// ]]>
</script>";

Thank you amariussi for taking time and posting code to help.

I have achieved with custom java script function and it needed to tweak:
custom/modules/Leads/Metadata/editvardefs.php with :


 'includes' => 
      array (
        0 => 
        array (
          'file' => 'custom/include/javascript/leadedits.js',
        ),
      ),

Also adding call to custom function in Save button with adding code before width array in same file:


 'form' => 
      array (
        'buttons' => 
        array (
          0 => 
          array (
            'customCode' => '<input title="Save [Alt+S]" accessKey="S" onclick="this.form.action.value=\'Save\'; return check_custom_data();" type="submit" name="button" value="Save">',
          ),
        ),
      ),

And then in javascript file


function check_custom_data()
{
	 var mob1 = document.getElementById('phone_mobile').value.length;
	 var email1 = document.getElementById('Leads0emailAddress0').value.length;
	 if(mob1 == 0 && email1== 0)
	 {
	 alert('Either email or mobile number must be entered to get data saved!');
	 return false;
	}
	else
	{
	return check_form('EditView');	
	}
}

As you seems having implementation of web-to-lead form, i wonder if you can guide what architecture need to be used suppose i have wordpress form and i need data saved directly as lead.

Regards,
Harshad

I’m created one module in that create one filed of radio button and apply it to YES or NO Radio Button
when I click on NO then visible anothe field and click on yes then not not Visible field.

I’m Create Link page Custom/module/metadata/(Module_name)/
‘includes’ =>
array (
0 =>
array (
‘file’ => ‘modules/eyd_SchoolCalling/SchoolCalling.js’,
),
),
And then Crete JS Page In Module/(Module_name)/

How Can I Apply Validation in that JS page

$(document).ready(function() {
var rv = $(‘input[name=whether_anapana_conducted]:checked’).val();
var $dval = $( “#call_status” ).val();
showfield_whether_anapana_conducted();
showfield_whether_workshop_conducted_c();
showfield_call_status($dval, rv);

 $("#call_status").change(function() {
  	showfield_call_status($(this).val(), rv);
});

 $('input[name=whether_anapana_conducted]:radio').click(function () { 
 	showfield_whether_anapana_conducted();
  });

  $('input[name=whether_workshop_conducted_c]:radio').click(function () { 
 	showfield_whether_workshop_conducted_c();
  });		
  function showfield_whether_anapana_conducted()
  {
  	var rv = $('input[name=whether_anapana_conducted]:checked').val();
		if(rv == 'No')
		{
			$("#reason_c").hide();
			//$("#reason_c option").prop("selected", false);
			$('#reason_c_label').html('');
		}
		else
		{
			$("#reason_c").show();
			$('#reason_c_label').html('If No, Select the Reason:');
		}
  }
  function showfield_whether_workshop_conducted_c()
  {
  	var rv = $('input[name=whether_workshop_conducted_c]:checked').val();
	
		if(rv == 'No')
		{
			$("#workshop_happening_date_c").hide();
			$("#workshop_happening_date_c_trigger").hide();
			$("#workshop_happening_location_c").hide();
			$("#workshop_type_c").hide();
			$('#workshop_happening_date_c_label').html('');
			$('#workshop_happening_location_c_label').html('');
			$('#workshop_type_c_label').html('');
		}
		else
		{
			$("#workshop_happening_date_c").show();
			$("#workshop_happening_date_c_trigger").show();
			$("#workshop_happening_location_c").show();
			$("#workshop_type_c").show();
			$('#workshop_happening_date_c_label').html('Workshop happening date:');
			$('#workshop_happening_location_c_label').html('Workshop happening location:');
			$('#workshop_type_c_label').html('Workshop type:');
		}
  }
  function showfield_call_status($dval, rv)
  {
  	
    if($dval == 'Not_Connected' )
    {
    	$( "[id=whether_anapana_conducted]" ).closest( "label" ).hide();
   		$( "[id=anapana_practice_type]" ).closest( "label" ).hide();
		$("#reason_c").hide();
		
		$('#reason_c_label').html('');
		$('#whether_anapana_conducted_label').html('');
		$('#anapana_practice_type_label').html('');
    }
    else
    {
    	$( "[id=whether_anapana_conducted]" ).closest( "label" ).show();
    	$( "[id=anapana_practice_type]" ).closest( "label" ).show();
    	$("#reason_c").show();
    	$('#reason_c_label').html('If No, Select the Reason:');
    	if(rv == 'No')
    	{
    		$("#reason_c").hide();
    		$('#reason_c_label').html('');
    	}
    	
		$('#whether_anapana_conducted_label').html('whether anapana conducted:');
		$('#anapana_practice_type_label').html('Anapana Practice Type ?:');
    }	
  }

});