-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformy.min.js
1 lines (1 loc) · 5.33 KB
/
formy.min.js
1
function validate(e){return e.stopPropagation(),e.preventDefault(),nameField.hasAttribute("required")&&nameField.value.length<3?(nameField.focus(),passedValidation=!1,$(nameField).prev().children().addClass("invalid"),$(nameField).addClass("invalid"),$(nameField).next().addClass("invalid"),!1):($(nameField).prev().children().removeClass("invalid"),$(nameField).removeClass("invalid"),$(nameField).next().removeClass("invalid"),passedValidation=!0,emailField.hasAttribute("required")&&emailField.value.length<5?(emailField.focus(),passedValidation=!1,$(emailField).prev().children().addClass("invalid"),$(emailField).addClass("invalid"),$(emailField).next().addClass("invalid"),!1):($(emailField).prev().children().removeClass("invalid"),$(emailField).removeClass("invalid"),$(emailField).next().removeClass("invalid"),passedValidation=!0,phoneField.hasAttribute("required")&&phoneField.value.length<9?(phoneField.focus(),passedValidation=!1,$(phoneField).prev().children().addClass("invalid"),$(phoneField).addClass("invalid"),$(phoneField).next().addClass("invalid"),!1):($(phoneField).prev().children().removeClass("invalid"),$(phoneField).removeClass("invalid"),$(phoneField).next().removeClass("invalid"),passedValidation=!0,messageField.hasAttribute("required")&&messageField.value.length<12?(messageField.focus(),$(messageField).prev().children().addClass("invalid"),$(messageField).addClass("invalid"),$(messageField).next().addClass("invalid"),!1):($(messageField).prev().children().removeClass("invalid"),$(messageField).removeClass("invalid"),$(messageField).next().removeClass("invalid"),void 0))))}var nameField=document.querySelector("#name"),emailField=document.querySelector("#email"),phoneField=document.querySelector("#phone"),messageField=document.querySelector("#message"),passedValidation=!1;$(document).ready(function(){$("input, textarea").focus(function(){$(this).prev().children().addClass("active"),$(this).next().addClass("active"),$(this).parent().find(".lenghtCounter").show()}).blur(function(){$("svg").removeClass("active invalid"),$(this).removeClass("invalid"),$("label").removeClass("active invalid"),$(".lenghtCounter").hide()}),$(".input-field i").click(function(){$(this).next().focus()}),$("input, textarea").blur(function(){0!=$(this).val().length?$(this).next().addClass("filled"):$(this).next().removeClass("filled")}),$(phoneField).keydown(function(e){var a=e.charCode||e.keyCode||0;return $phone=$(this),char=$phone.val().charAt(1),8!==a&&9!==a&&"3"!==char&&($phone.attr("maxlength","18"),0===$phone.val().length&&$phone.val($phone.val()+"+"),2===$phone.val().length&&$phone.val($phone.val()+" ("),7===$phone.val().length&&$phone.val($phone.val()+") "),12===$phone.val().length&&$phone.val($phone.val()+"-"),15===$phone.val().length&&$phone.val($phone.val()+"-")),8!==a&&9!==a&&"3"==char&&($phone.attr("maxlength","19"),0===$phone.val().length&&$phone.val($phone.val()+"+"),3===$phone.val().length&&$phone.val($phone.val()+" ("),8===$phone.val().length&&$phone.val($phone.val()+") "),13===$phone.val().length&&$phone.val($phone.val()+"-"),16===$phone.val().length&&$phone.val($phone.val()+"-")),8==a||9==a||46==a||a>=48&&a<=57||a>=96&&a<=105})}),$(document).delegate("input","keyup",function(){var e=$(this),a=$(this).parent().find(".lenghtCounter span");e.bind("keydown",function(){setTimeout(function(){a.text(e.val().length+"/"+e.attr("maxlength"))},4),$(this).val().length==$(this).attr("maxlength")?a.css("color","#3498db"):a.css("color","inherit")})}),$.each($("#message"),function(){var e=this.offsetHeight-this.clientHeight,a=function(a){$(a).css("height","auto").css("height",a.scrollHeight+e)};$(this).on("keyup input",function(){a(this)})}),$("#formy").submit(function(e){var a;if(passedValidation){$("#result").html(""),$("#result").hide();var l=$(this).serialize();a=$.ajax({url:"/php/formy.php",type:"post",data:l}),a.done(function(e,a,l){$("#result").removeClass("invalidRequest"),$("#result").html('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.2 19.2"><path class="a" d="M10,0.4A9.6,9.6,0,1,0,19.6,10,9.6,9.6,0,0,0,10,.4Zm0,17.2A7.6,7.6,0,1,1,17.6,10,7.6,7.6,0,0,1,10,17.6ZM7.5,9.75A1.64,1.64,0,0,0,9,8,1.64,1.64,0,0,0,7.5,6.25,1.64,1.64,0,0,0,6,8,1.64,1.64,0,0,0,7.5,9.75Zm5,0A1.64,1.64,0,0,0,14,8a1.64,1.64,0,0,0-1.5-1.75A1.64,1.64,0,0,0,11,8,1.64,1.64,0,0,0,12.5,9.75Zm1.84,1.59a0.76,0.76,0,0,0-1,.32A3.61,3.61,0,0,1,10,13.25a3.62,3.62,0,0,1-3.33-1.59,0.75,0.75,0,0,0-1.34.68A5.06,5.06,0,0,0,10,14.75a5.05,5.05,0,0,0,4.67-2.41A0.75,0.75,0,0,0,14.34,11.34Z" transform="translate(-0.4 -0.4)"/></svg>Submitted successfully!'),$("#result").show().delay(5e3).fadeOut("slow"),$("#formy")[0].reset()}),a.fail(function(){$("#result").addClass("invalidRequest"),$("#result").html('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.2 19.2"><path class="a" d="M10,0.4A9.6,9.6,0,1,0,19.6,10,9.6,9.6,0,0,0,10,.4Zm0,17.2A7.6,7.6,0,1,1,17.6,10,7.6,7.6,0,0,1,10,17.6Zm2.5-7.85A1.64,1.64,0,0,0,14,8a1.64,1.64,0,0,0-1.5-1.75A1.64,1.64,0,0,0,11,8,1.64,1.64,0,0,0,12.5,9.75Zm-5,0A1.64,1.64,0,0,0,9,8,1.64,1.64,0,0,0,7.5,6.25,1.64,1.64,0,0,0,6,8,1.64,1.64,0,0,0,7.5,9.75Zm2.5,1.5a5.06,5.06,0,0,0-4.67,2.41,0.75,0.75,0,0,0,.34,1,0.76,0.76,0,0,0,.34.08,0.75,0.75,0,0,0,.67-0.41A3.59,3.59,0,0,1,10,12.75a3.62,3.62,0,0,1,3.33,1.59,0.75,0.75,0,0,0,1.34-.67A5.06,5.06,0,0,0,10,11.25Z" transform="translate(-0.4 -0.4)"/></svg>There is error while submit'),$("#result").show().delay(5e3).fadeOut("slow")})}});