javascript - Validating form fields before submit -
i have function frmvalidator using compare 2 input fields. thing got 2 fields required , user need fill 1 or both can not leave 2 input unfilled.
here i've been working on:
<form id="contact_form" action="contact.php" method="post"> <label>rfc ** </label> <input class="fiscal-input" type="text" name="rfc" placeholder=""> <label> ó curp **</label> <input class="fiscal-input" type="text" name="curp" placeholder=""> </form> frmvalidator.addvalidation("curp","regexp=^[a-z]{1}[aeiou]{1}[a-z]{2}[0-9]{2}(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[hm]{1}(as|bc|bs|cc|cs|ch|cl|cm|df|dg|gt|gr|hg|jc|mc|mn|ms|nt|nl|oc|pl|qt|qr|sp|sl|sr|tc|ts|tl|vz|yn|zs|ne)[b-df-hj-np-tv-z]{3}[0-9a-z]{1}[0-9]{1}$","por favor ingrese un curp válido."); frmvalidator.addvalidation("rfc","regexp=^([a-z,Ñ,&]{3,4}([0-9]{2})(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[a-z|\d]{3})$","por favor ingrese un curp válido.");
the 2 regex control input alphanumeric digits. need javascript function allows fill 1 or both no keep them empty.
can guys guide me this?
you can use simple form submit handler custom validation. posting example code below.
document.getelementbyid( 'contact_form' ).addeventlistener('submit', function(e) { var fields = document.queryselectorall( '#contact_form .fiscal-input'); var value = ''; for( var i=0, len = fields.length; i<len; i++ ) { value = value || fields[i].value; } if(!value) { e.preventdefault(); } });
Comments
Post a Comment