Fórum

AUI form validation-either of the field required condition

Smilelws2010 lwz, modificado 8 Anos atrás.

AUI form validation-either of the field required condition

Regular Member Postagens: 160 Data de Entrada: 16/12/10 Postagens Recentes
Hi

I have two input aui fields with digits only condition applied. Either one of the input field should be entered, like SSN or Employee ID on submitting the form

How can you achieve this using aui form field validation. How can I add the above condition to my below script..

Thanks in advance
Smile


<aui:script>
AUI().use('aui-base','aui-form-validator',   function(A) {

		  var DEFAULTS_FORM_VALIDATOR = A.config.FormValidator;

		 var rules = {
			      <portlet:namespace />lastName: {
			      		required: true,
				        alpha:true,
			      },

		     	 <portlet:namespace />employeeId: {
			       		digits:true,
			       		maxLength:12
			     },
			     <portlet:namespace />ssn: {
			       		digits:true,
			       		maxLength:4
			     }
		  };
		
	    var fieldStrings = {
		      	<portlet:namespace />lastName: {
		        		required: 'Please enter your last name.'
		        },

	    };
	    new A.FormValidator(
	      {
	        boundingBox: '#<portlet:namespace />fm',
	        fieldStrings: fieldStrings,
	        rules: rules,
	        showAllMessages: false
	      }
	    );	    
  }
);

</aui:script>

I tried changing button type submit to click  and did an on click function like below

               
		 <!-- JS check to ensure either  Student ID or HR employee ID is filled and both are not empty  -->
         A.one('#<portlet:namespace />lookupSearch').on('click',function(){

                 var ssnVal= A.one('#<portlet:namespace />ssn').get('value');
                 var hrEmployeeIdVal = A.one('#<portlet:namespace />employeeId').get('value');
                                  
                 if( ((ssnVal.length==0) &amp;&amp; (employeeIdVal.length==0))  &amp;&amp;  (lName.length==0)  ) {

                                 A.one('#invalidParamMessage').html('<p class="portlet-msg-error">Invalid input\. Please enter data in required fields.</p>'); 

                 }else{
                      goSubmit()
            }

         });
         
         function goSubmit(){
 				alert("about to submit");   
                form.submit; // bear the syntax, but this did work.. what happedned was it was checking only the if condition above and ignore AUI digits and alpha..


         } 







thumbnail
Pavel Savinov, modificado 8 Anos atrás.

RE: AUI form validation-either of the field required condition

Junior Member Postagens: 54 Data de Entrada: 29/05/15 Postagens Recentes
Hi there,

You can create your own rules for SSN or any other type of data, like this:

A.mix(
    DEFAULTS_FORM_VALIDATOR.RULES,
    {
    	ruleCheckSSN:function (val, fieldNode, ruleValue) {
        	var result = false;
        	
			/* check val here, for example, with regexp */

        	return result;
        },
    },
    true
);

A.mix(
    DEFAULTS_FORM_VALIDATOR.STRINGS,
    {
        ruleCheckSSN:"Please enter valid SSN",
    },

    true
);


And after that just use your new rule:

<portlet:namespace />ssn: {
	digits:true,
	maxLength:4,
	ruleCheckSSN:true
}
Smilelws2010 lwz, modificado 8 Anos atrás.

RE: AUI form validation-either of the field required condition

Regular Member Postagens: 160 Data de Entrada: 16/12/10 Postagens Recentes
HI Pavel

Thanks for the reply.

I know we can have custom rule per field. My question is more like

You can enter value either only SSN or on Employee ID (two input text boxes), On submit, i have to check either one of the field is entered or not.

I can apply required on both the text boxes ( as you can leave either one of the text boxes blank).

How can you right a custom rule on two aui input text fields and compare whether there is value or not.

Thank You
Smile
Smilelws2010 lwz, modificado 8 Anos atrás.

RE: AUI form validation-either of the field required condition

Regular Member Postagens: 160 Data de Entrada: 16/12/10 Postagens Recentes
To be more accurate, how can we write a custom rule in aui form validator to read the values of two fields and do a conditional check to see whether they are empty or not.

                 var ssnVal= A.one('#<portlet:namespace />ssn').get('value');
                 var hrEmployeeIdVal = A.one('#<portlet:namespace />employeeId').get('value');
                                  
                 if( ((ssnVal.length==0) &amp;&amp; (employeeIdVal.length==0))  ) {

                                 A.one('#invalidParamMessage').html('<p class="portlet-msg-error">Invalid input\. Required value in one of the fields.</p>'); 
                 }
                 else{
                     goSubmit()
                }
Harsh Joshi, modificado 8 Anos atrás.

RE: AUI form validation-either of the field required condition

Junior Member Postagens: 27 Data de Entrada: 28/01/15 Postagens Recentes
AUI().use(
'aui-form-validator',
function(A) {
new A.FormValidator(
{
boundingBox: '#(id of your form)',
rules: {
name: {
required: true
}
}
}
);
}
);
gary b, modificado 6 Anos atrás.

RE: AUI form validation-either of the field required condition

Junior Member Postagens: 81 Data de Entrada: 02/02/13 Postagens Recentes
Did you find any solution for this?
I also need to put 'or' condition between two fields.