Jquery Validation in CkEditor - php

I have seen this example and applied it in my code yet nothing worked, its not working.
Referral ans-1
Referral ans-2
I need to apply jquery validation in ckeditor and I have seen all those past examples even I have mentioned those links above, by doing that step my validation is still not working.
Here is my HTML Code
<div class="elementbox">
<label class="form-label">Content<span class="required">*</span></label>
<div class="controls">
<textarea name="content_body" id="content_body" rows="10" cols="80"><?php echo $content_body; ?></textarea>
</div>
</div>
<script>
var url = "<?php echo base_url(); ?>";
CKEDITOR.replace( 'content_body',{
//extraPlugins: 'imageuploader'
} );
</script>
My Jquery validation code
$("#add_content_pages").validate({
ignore: [],
debug: false,
rules: {
title: {
required: true
},
content_id: {
required: true
},
content_body:{
required: function()
{
CKEDITOR.instances.content_body.updateElement();
}
}
},
messages: {
title: {
required: "Please enter Title"
},
content_id: {
required: "Please Select Content Type"
},
content_body: {
required: "Please enter Content"
}
},
errorPlacement: function (error, element) {
var attr_name = element.attr('name');
if (attr_name == 'type') {
error.appendTo('.type_err');
} else {
error.insertAfter(element);
}
}
});
Any Solution what I am missing?

please check this code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js"></script>
then html form
<form action="" method="post" id="check_form">
<div class="elementbox">
<label class="form-label">Content<span class="required">*</span></label>
<div class="controls">
<textarea name="content_body" id="content_body" rows="10" cols="80"></textarea>
<div id="error_check_editor"></div>
</div>
</div>
<script>
CKEDITOR.replace('content_body');
</script>
<br/>
<input name="submit" type="submit" value="Submit" class="button" id="submit"/>
</form>
then script
<script>
$.validator.addMethod("check_ck_add_method",
function (value, element) {
return check_ck_editor();
});
function check_ck_editor() {
if (CKEDITOR.instances.content_body.getData() == '') {
return false;
}
else {
$("#error_check_editor").empty();
return true;
}
}
$(document).ready(function () {
$("#check_form").validate(
{
ignore: [],
debug: false,
errorPlacement: function (error, element) {
if (element.attr("name") == "content_body") {
error.insertAfter(element);
}
},
rules: {
content_body: {
check_ck_add_method: true
}
},
messages: {
content_body: {}
}
});
});
</script>

Related

i want to log in a user with enter key

I want to log in a user by pressing the enter key. when I press the enter key it clears the form value. and by clicking with the mouse on the sign in button it allows the user to log in and redirect on the home page.
here is my login.min.js
var Login = function () {
var e = function () {
$(".login-form").validate({
errorElement: "span",
errorClass: "help-block",
focusInvalid: !1,
rules: {username: {required: !0}, password: {required: !0}, remember: {required: !1}},
messages: {username: {required: "Username is required."}, password: {required: "Password is required."}},
invalidHandler: function (e, r) {
$(".alert-danger", $(".login-form")).show()
},
highlight: function (e) {
$(e).closest(".form-group").addClass("has-error")
},
success: function (e) {
e.closest(".form-group").removeClass("has-error"), e.remove()
},
errorPlacement: function (e, r) {
e.insertAfter(r.closest(".input-icon"))
},
submitHandler: function (e) {
e.submit()
}
}), $(".login-form input").keypress(function (e) {
return 13 == e.which ? ($(".login-form").validate().form() && $(".login-form").submit(), !1) : void 0
})
}, r = function () {
$(".forget-form").validate({
errorElement: "span",
errorClass: "help-block",
focusInvalid: !1,
ignore: "",
rules: {email: {required: !0, email: !0}},
messages: {email: {required: "Email is required."}},
invalidHandler: function (e, r) {
},
highlight: function (e) {
$(e).closest(".form-group").addClass("has-error")
},
success: function (e) {
e.closest(".form-group").removeClass("has-error"), e.remove()
},
errorPlacement: function (e, r) {
e.insertAfter(r.closest(".input-icon"))
},
submitHandler: function (e) {
e.submit()
}
}), $(".forget-form input").keypress(function (e) {
return 13 == e.which ? ($(".forget-form").validate().form() && $(".forget-form").submit(), !1) : void 0
}), jQuery("#forget-password").click(function () {
jQuery(".login-form").hide(), jQuery(".forget-form").show()
}), jQuery("#back-btn").click(function () {
jQuery(".login-form").show(), jQuery(".forget-form").hide()
})
}, i = function () {
function e(e) {
if (!e.id)return e.text;
var r = $('<span><img src="../assets/global/img/flags/' + e.element.value.toLowerCase() + '.png" class="img-flag" /> ' + e.text + "</span>");
return r
}
jQuery().select2 && $("#country_list").size() > 0 && ($("#country_list").select2({
placeholder: '<i class="fa fa-map-marker"></i> Select a Country',
templateResult: e,
templateSelection: e,
width: "auto",
escapeMarkup: function (e) {
return e
}
}), $("#country_list").change(function () {
$(".register-form").validate().element($(this))
})), $(".register-form").validate({
errorElement: "span",
errorClass: "help-block",
focusInvalid: !1,
ignore: "",
rules: {
fullname: {required: !0},
email: {required: !0, email: !0},
address: {required: !0},
city: {required: !0},
country: {required: !0},
username: {required: !0},
password: {required: !0},
rpassword: {equalTo: "#register_password"},
tnc: {required: !0}
},
messages: {tnc: {required: "Please accept TNC first."}},
invalidHandler: function (e, r) {
},
highlight: function (e) {
$(e).closest(".form-group").addClass("has-error")
},
success: function (e) {
e.closest(".form-group").removeClass("has-error"), e.remove()
},
errorPlacement: function (e, r) {
"tnc" == r.attr("name") ? e.insertAfter($("#register_tnc_error")) : 1 === r.closest(".input-icon").size() ? e.insertAfter(r.closest(".input-icon")) : e.insertAfter(r)
},
submitHandler: function (e) {
e[0].submit()
}
}), $(".register-form input").keypress(function (e) {
return 13 == e.which ? ($(".register-form").validate().form() && $(".register-form").submit(), !1) : void 0
}), jQuery("#register-btn").click(function () {
jQuery(".login-form").hide(), jQuery(".register-form").show()
}), jQuery("#register-back-btn").click(function () {
jQuery(".login-form").show(), jQuery(".register-form").hide()
})
};
return {
init: function () {
e(), r(), i()
}
}
}();
jQuery(document).ready(function () {
Login.init()
});
and my login_form.tpl
<body class=" login">
<!-- BEGIN LOGO -->
<div class="logo">
<a href="#">
<img src=" {$site_root}application/views/admin/assets/layouts/layout/img/logo.png"
alt="logo" /> </a>
</div>
<!-- END LOGO -->
<!-- BEGIN LOGIN -->
<div class="content">
<!-- BEGIN LOGIN FORM -->
<form class="login-form" action="{$data.action}" method="post">
<h3 class="form-title font-green">Sign In</h3>
<div class="alert alert-danger display-hide" style='display:{if $error != ''}block{/if}'>
<button class="close" data-close="alert"></button>
<span> {if $error != ''}{$error}{else}Enter any username and password.{/if}</span>
</div>
<div class="form-group">
<!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
<label class="control-label visible-ie8 visible-ie9">{l i='field_nickname' gid='ausers'}</label>
<input class="form-control form-control-solid placeholder-no-fix" type="text" autocomplete="off" placeholder="Username" name="nickname" /> </div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">{l i='field_password' gid='ausers'}</label>
<input class="form-control form-control-solid placeholder-no-fix" type="password" autocomplete="off" placeholder="Password" name="password" /> </div>
<div class="form-actions">
<input type="submit" name="btn_login" value="{l i='btn_login' gid='start' type='button'}" class="btn green uppercase btn-block">
</div>
</form>
i have tried different solutions but did'nt succeed.
$('.form-group').keydown(function() {
var key = e.which;
if (key == 13) {
$('.form-group').submit();
}
});
Thank you for your help but finally, I find the solution I just needed to make some changes.
$("input").keypress(function (e) {
if (e.which == 13) {
$('input[name="btn_login"]').click();
return false;
}

Google Invisible ReCaptcha on multiple forms in a same page - PHP

I am following this Invisible ReCaptcha doc on my php website: http://www.pinnacleinternet.com/installing-invisible-recaptcha/ everything works well. but while implement on multiple forms in a same page then Captcha only work on first form and not sure what’s happening with the second form, I curious to know how its work with multiple forms in a single page.
Or anyone please suggest a working doc for multiple forms?
//this is #Geordy's javascript portion modified according to jquery.validate
<script type="text/javascript">
$().ready(function() {
var demo1Call = 0;
var demo2Call = 0;
// validate and submit 1st form
$("#demo-form1").validate({
rules: {
pass: {
required: true,
pwcheck: true,
minlength: 5
},
},
messages: {
pass: {
required: "Please provide a password",
pwcheck: "<br />*Minimum length 8<br />*Maximum length 24<br />*Atleast a digit<br />*Atleast an upper case<br />*Atleast a lowercase<br />*Atleast a special character from these !##$%",
minlength: "Your password must be at least 5 characters long"
},
},
success: function(error){
console.log("Successfully validated");
},
submitHandler: function(form) {
demo1Call++;
if(demo1Call==1){
widgetId1 = grecaptcha.render('recaptcha1', {
'sitekey' : '<?php echo $config['client-key']; ?>',
'callback' : onSubmit1,
'size' : "invisible"
});
}
grecaptcha.reset(widgetId1);
grecaptcha.execute(widgetId1);
},
});
//validate and submit 2nd form
$("#demo-form2").validate({
rules: {
pass: {
required: true,
pwcheck: true,
minlength: 5
},
},
messages: {
pass: {
required: "Please provide a password",
pwcheck: "<br />*Minimum length 8<br />*Maximum length 24<br />*Atleast a digit<br />*Atleast an upper case<br />*Atleast a lowercase<br />*Atleast a special character from these !##$%",
minlength: "Your password must be at least 5 characters long"
},
},
success: function(error){
console.log("Successfully validated");
},
submitHandler: function(form) {
demo2Call++;
if(demo2Call==1){
widgetId2 = grecaptcha.render('recaptcha2', {
'sitekey' : '<?php echo $config['client-key']; ?>',
'callback' : onSubmit2,
'size' : "invisible"
});
}
grecaptcha.reset(widgetId2);
grecaptcha.execute(widgetId2);
},
});
});
$.validator.addMethod("pwcheck", function(value) {
var pattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%])[0-9A-Za-z!##$%]{8,24}$/;
return pattern.test(value);
});
function onSubmit1(token){
document.getElementById("demo-form1").submit();
};
function onSubmit2(token){
document.getElementById("demo-form2").submit();
};
</script>
The below code work for me
<?php
$config = require('config.php');
?>
<html>
<head>
<title>reCAPTCHA demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Boostrap Validator -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
var demo1Call = 0;
var demo2Call = 0;
$('#demo-form1').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
console.log("validation failed");
} else {
// everything looks good!
demo1Call++;
e.preventDefault();
console.log("validation success");
if(demo1Call==1)
{
widgetId1 = grecaptcha.render('recaptcha1', {
'sitekey' : '<?php echo $config['client-key']; ?>',
'callback' : onSubmit1,
'size' : "invisible"
});
}
grecaptcha.reset(widgetId1);
grecaptcha.execute(widgetId1);
}
});
$('#demo-form2').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
console.log("validation failed");
} else {
// everything looks good!
demo2Call++;
e.preventDefault();
console.log("validation success");
if(demo2Call==1)
{
widgetId2 = grecaptcha.render('recaptcha2', {
'sitekey' : '<?php echo $config['client-key']; ?>',
'callback' : onSubmit2,
'size' : "invisible"
});
}
grecaptcha.reset(widgetId2);
grecaptcha.execute(widgetId2);
}
});
});
function onSubmit1(token){
document.getElementById("demo-form1").submit();
};
function onSubmit2(token){
document.getElementById("demo-form2").submit();
};
</script>
</head>
<body>
<div class="container">
<br>
<div class="row">
<div class="col-md-5 col-md-offset-3">
<form id="demo-form1" data-toggle="validator" role="form" action="admin.php" method="POST" >
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Geordy James" required/>
</div>
<div id='recaptcha1' ></div>
<button class="btn btn-block btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
<br>
<div class="row">
<div class="col-md-5 col-md-offset-3">
<form id="demo-form2" data-toggle="validator" role="form" action="admin2.php" method="POST" >
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Geordy James" required/>
</div>
<div id='recaptcha2' ></div>
<button class="btn btn-block btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
</div>
<script src="https://www.google.com/recaptcha/api.js" async defer ></script>
</body>
</html>
I used Unofficial Google Invisible reCAPTCHA PHP library in this program and you can download it from https://github.com/geordyjames/google-Invisible-reCAPTCHA . If this method doesn't work for you please comment below.

How to validate name="novs[78][] using jquery validation?

<?php
for ($i=0; $i < $total_vehicles; $i++) {
$id=$edtVehicle[$i]->id;
$chasis=$edtVehicle[$i]->chasis;
?>
//HTML CONTENT "novs[][]" want to validate it using jquery validation
<div class="controls content contold">
<input type="hidden" name="hid_novs[]" value="<?php echo $id;?>">
<input type="text" name="novs[<?php echo $id;?>][]" id="novs_<?php echo $i;?>" value="<?php echo $chasis;?>">
<button type="button" class="removebtn"><i class="fa fa-times"></i></button>
<span class="span_error"></span>
</div>
<?php
}
?>
JQUERY VALIDATION
$("#add_user_form").validate({
"novs[]":{ required:true, },
messages: { "novs[]":{ required : "Please fill this value" }, }
});
I Solved it with this way, it's solved now thank you.
$("input[name^='novs_'" ).each(function(){
$(this).rules("add", {
required: true,
messages: {
required: "Fill a valid value"
}
});
});
Hope fully this will helps you to get id
$('.removebtn').click(function() {
//console.log( $(this).siblings('.removebtn'));
//console.log($(this).siblings("input").attr('id'));
var found = false;
$(this).siblings("input").each(function (i, name) {
// Check if field is empty or not
console.log(name);
if (!found && jQuery(name).val() == '') {
alert('Please enter value')
found = true;
};
});
return !found;
});
Fiddle

Firebug shows POST info, but I can't echo

I have a form that seems to POST successfully after submitting via AJAX. Under "NET" in Firebug the posted info displays properly. But I cannot echo this data at all.
I'm trying this, which is the 1st field in my form: <?php echo $_POST['sun_exposure']; ?>
Does this have anything to do with the form's ACTION not having a url?
Also, I'm trying to POST data to the same page, and not a PHP file. Maybe this is part of the problem? This is also a Wordpress page, and not a file.
Here's the live url: http://www.richmindonline.com/container-creations/personal-creations-assistant/
Here's the form code:
<form id="quotation_form" name="vdmQuotationForm" action="#" method="post">
<div id="page1">
<div id="step0_header" class="steps-headers" onClick="step0ToggleClick();">Step 1<span style="margin-left:30px;">Sun Exposure</span></div>
<div id="step0" class="quotation-steps"><strong>Describe the sun exposure of your planter...</strong><br/>
<div class="radio-container"><input onchange="go_to_next_step();" type="radio" name="sun_exposure[]" id="full_sun" value="Full Sun"<?php checked( 'Full Sun',$_POST['sun_exposure[]']); ?> /><label class="label-quotation-steps">Full Sun</label><br class="clear"/></div>
<div class="radio-container"><input onchange="go_to_next_step();" type="radio" name="sun_exposure[]" id="part_sun" value="Part Sun"<?php checked( 'Part Sun',$_POST['sun_exposure[]']); ?> /><label class="label-quotation-steps">Part Sun</label><br class="clear"/></div>
<div class="radio-container"><input onchange="go_to_next_step();" type="radio" name="sun_exposure[]" id="full_shade" value="Full Shade"<?php checked( 'Full Shade',$_POST['sun_exposure[]']); ?> /><label class="label-quotation-steps">Full Shade</label><br class="clear"/></div>
</div>
</div>
</div>
</form>
Here's the AJAX request, which is wrapped up in the jQuery validate script:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#quotation_form').validate({ // initialize the plugin
rules: {
'sun_exposure[]': {
required: true,
},
'height[]': {
required:true,
},
'diameter[]': {
required:true,
},
'shape[]': {
required:true,
},
'placement[]': {
required:true,
},
},
messages: {
'sun_exposure[]': {
required: 'Please choose a sun exposure for your plant',
},
'height[]': {
required: 'Please choose the height of your planter'
},
'diameter[]': {
required: 'Please choose the diamter of your planter'
},
'shape[]': {
required: 'Please choose the shape of your planter'
},
'placement[]': {
required: 'Please choose the placement of your planter'
},
},
errorPlacement: function (error, element) {
alert(error.text());
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
var form_data = $("#quotation_form").serialize();
$.ajax({
url: "http://www.richmindonline.com/container-creations/personal-creations-assistant/",
type: 'POST',
data: form_data,
cache: true,
success: function(data) {
alert(data);
}
});
$('#page1').hide();
$('html, body').animate({ scrollTop: $('body').offset().top }, 10);
$('#page2').show();
$('.intro').hide();
$('.to_page1').show();
return false;
}
}); // end .validate()
});
</script>

Must submit form twice

I have the following script. It works in jsFiddle
The problem is when I fill in the title AND the text box I have to submit TWICE.
Does someone have an idea of what I'm doing wrong?!
form:
<form id="formid" class="form" method="POST" action="/">
<div>
<label class="title">Title</label>
<div id="titleError"></div>
<input type="text" id="title" name="title" value="">
</div>
<div>
<label class="title">Text</label>
<div id="textError"></div>
<textarea name="text" id="text" rows="14" cols="50"></textarea><br />
</div>
<div>
<input class="btn btn-success" type="submit" id="submitButton" name="submitButton" value="Submit">
</div>
JS:
<script type='text/javascript'>
$(document).ready(function() {
$("#formid").submit( function(event) {
tinyMCE.triggerSave();
var title = $('#title').val();
var text = $('#text').val();
if( title.length === 0 || text.length === 0 ){
if( title.length === 0 ){
$("#titleError").html("<p>Title verplicht</p>");
event.preventDefault();
}
if( text.length === 0 ){
$("#textError").html("<p>Text verplicht</p>");
event.preventDefault();
}
$("html, body").animate({ scrollTop: 0 }, 600);
}
else{
tinyMCE.triggerSave();
/* stop form from submitting normally */
event.preventDefault();
/* Send the data using post */
var posting = $.post( 'http://domain.nl/admin/pages/create', {
title: $('#title').val(),
text: $('#text').val()
});
/* Put the results in the show-content div */
posting.done(function( data ) {
//alert(data);
$.ajax({
url: "<?php echo base_url() ?>/admin/pages",
type: 'GET',
success: function(data) {
$("#show-content").hide().html( data).fadeIn(1500);
}
,
error: function() {
alert("error");
}
});
});
}
});
});
</script>
The solution:
I add this
tinyMCE.triggerSave();
after
$("#formid").submit( function(event) {
and now it works correctly!

Categories