[ captPicard @ 19.03.2017. 14:54 ] @
Molim pomoć, kako da spojim ova dva .js, dakle moram dodati u bootstrap validator da provjeri captcha prije nego dozvolim form submit. Hvala. prvi.js Code: $(document).ready(function() { $('#contact_form').bootstrapValidator({ // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { first_name: { validators: { stringLength: { min: 2, }, notEmpty: { message: 'Upišite prezime' } } }, last_name: { validators: { stringLength: { min: 2, }, notEmpty: { message: 'Upišite ime' } } }, email: { validators: { notEmpty: { message: 'Upišite mail adresu' }, emailAddress: { message: 'Upišite valjanu mail adresu' } } }, phone: { validators: { notEmpty: { message: 'Upišite kontakt broj' } } }, address: { validators: { stringLength: { min: 8, }, notEmpty: { message: 'Upišite adresu' } } }, city: { validators: { stringLength: { min: 4, }, notEmpty: { message: 'Upišite mjesto' } } }, oib: { validators: { notEmpty: { message: 'Upišite OIB' } } }, zip: { validators: { notEmpty: { message: 'Please supply your zip code' }, zipCode: { country: 'US', message: 'Please supply a vaild zip code' } } }, comment: { validators: { stringLength: { min: 10, max: 200, message:'Please enter at least 10 characters and no more than 200' }, notEmpty: { message: 'Please supply a description of your project' } } } } }) .on('success.form.bv', function(e) { $('#success_message').slideDown({ opacity: "show" }, "slow") // Do something ... $('#contact_form').data('bootstrapValidator').resetForm(); // Prevent form submission e.preventDefault(); // Get the form instance var $form = $(e.target); // Get the BootstrapValidator instance var bv = $form.data('bootstrapValidator'); // Use Ajax to submit form data $.post($form.attr('action'), $form.serialize(), function(result) { console.log(result); }, 'json'); } ); }); drugi.js Code: var cd; $(function(){ CreateCaptcha(); }); // Create Captcha function CreateCaptcha() { //$('#InvalidCapthcaError').hide(); var alpha = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); var i; for (i = 0; i < 6; i++) { var a = alpha[Math.floor(Math.random() * alpha.length)]; var b = alpha[Math.floor(Math.random() * alpha.length)]; var c = alpha[Math.floor(Math.random() * alpha.length)]; var d = alpha[Math.floor(Math.random() * alpha.length)]; var e = alpha[Math.floor(Math.random() * alpha.length)]; var f = alpha[Math.floor(Math.random() * alpha.length)]; } cd = a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f; $('#CaptchaImageCode').empty().append('<canvas id="CapCode" class="capcode" width="300" height="80"></canvas>') var c = document.getElementById("CapCode"), ctx=c.getContext("2d"), x = c.width / 2, img = new Image(); img.src = "https://pixelsharing.files.wor...eable-and-seamless-pattern.jpg"; img.onload = function () { var pattern = ctx.createPattern(img, "repeat"); ctx.fillStyle = pattern; ctx.fillRect(0, 0, c.width, c.height); ctx.font="46px Roboto Slab"; ctx.fillStyle = '#ccc'; ctx.textAlign = 'center'; ctx.setTransform (1, -0.12, 0, 1, 0, 15); ctx.fillText(cd,x,55); }; } // Validate Captcha function ValidateCaptcha() { var string1 = removeSpaces(cd); var string2 = removeSpaces($('#UserCaptchaCode').val()); if (string1 == string2) { return true; } else { return false; } } // Remove Spaces function removeSpaces(string) { return string.split(' ').join(''); } // Check Captcha function CheckCaptcha() { var result = ValidateCaptcha(); if( $("#UserCaptchaCode").val() == "" || $("#UserCaptchaCode").val() == null || $("#UserCaptchaCode").val() == "undefined") { $('#WrongCaptchaError').text('Unesite slova sa slike').show(); $('#UserCaptchaCode').focus(); } else { if(result == false) { $('#WrongCaptchaError').text('Neispravan unos, molimo ponovite.').show(); CreateCaptcha(); $('#UserCaptchaCode').focus().select(); } else { $('#UserCaptchaCode').val('').attr('place-holder','Unesite Captcha'); CreateCaptcha(); $('#WrongCaptchaError').fadeOut(100); $('#SuccessMessage').fadeIn(500).css('display','block').delay(5000).fadeOut(250); } } } |