-
Knutsford
Member
-
Posts: 92
Threads: 40
Joined: Dec 2016
Reputation:
0
04-28-2020, 07:12 AM
(This post was last modified: 04-28-2020, 09:41 AM by jreklund.)
I am trying to add in validation for just Alphabetic and spaces but something seems to be wrong with this. It is in MY_Form_validation.php
under /libraries. Can anyone see anything wrong with? It is version 3 of codeigniter
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Form_validation extends CI_Form_validation { protected $CI;
public function __construct() { $this->CI =& get_instance();
// Validation rules can be stored in a config file. $this->_config_rules = $rules;
// Automatically load the form helper $this->CI->load->helper('form');
// Set the character encoding in MB. if (function_exists('mb_internal_encoding')) { mb_internal_encoding($this->CI->config->item('charset')); }
log_message('debug', "Form Validation Class Initialized"); } function alpha_space($str) { $this->CI->form_validation->set_message('alpha_space', 'The %s may only contain Alpha Characters and Spaces.'); if (trim($str) !== "" & Is_a_Letter_or_Space_validation($str)) { return TRUE; } else { return FALSE; } } function Is_a_Letter_or_Space_validation($inputbox) {
$teststring = Trim($inputbox); if ($teststring == "") { return False;
}
$i = 0; while ($i <= strlen($teststring) - 1) { $c = strtolower(substr($teststring,$i,1));
if ($c <> "a" & $c <> "b" & $c <> "c" & $c <> "d" & $c <> "e" & $c <> "f" & $c <> "g" & $c <> "h" & $c <> "i" & $c <> "j" & $c <> "k" & $c <> "l" & $c <> "m" & $c <> "n" & $c <> "o" & $c <> "p" & $c <> "q" & $c <> "r" & $c <> "s" & $c <> "t" & $c <> "u" & $c <> "v" & $c <> "w" & $c <> "x" & $c <> "y" & $c <> "z" & $c <> " ") { return False; } $i = $i + 1; } return True; } }
The problem is in
Code: events: {
'click .submit-order-info': 'onSubmitClick'
},
initialize: function() {
},
el: '#order-info-form',
submit: function (callback) {
$.ajax({
url: DT_CONFIG.baseURL + 'ajax/submitorder', dataType: 'json', cache: false, timeout: 10000,
type: "POST",
data: this.$el.serialize(),
success: function (data) {
if (data.status === "success") {
callback(null, data);
} else {
callback(data.errors, data);
}
},
error: function () {
callback('GENERAL_ERROR');
}
});
},
onSubmitClick: function () {
var that = this;
this.$el.find(".form-errors").slideUp(500);
this.submit(function(err,data) {
if(err) {
var $formErrors = that.$el.find(".form-errors");
/* Errors found */
$formErrors.html("<h4>" + that.$el.find(".form-errors h4").text() + "</h4>" + err);
$formErrors.slideDown(500);
} else {
window.location = DT_CONFIG.baseURL + "step/shipping?purchase=" + data.orderData.order_id;
}
});
It is going into the else statement in onSubmitClick when there is an error
Thanks. I haven't added a new validater before so it is probably something obvious
-
jreklund
Administrator
-
Posts: 1,408
Threads: 3
Joined: Aug 2017
Reputation:
42
-
Knutsford
Member
-
Posts: 92
Threads: 40
Joined: Dec 2016
Reputation:
0
04-28-2020, 10:50 AM
(This post was last modified: 04-28-2020, 10:54 AM by Knutsford.)
(04-28-2020, 07:12 AM)Knutsford Wrote: I am trying to add in validation for just Alphabetic and spaces but something seems to be wrong with this. It is in MY_Form_validation.php
under /libraries. Can anyone see anything wrong with? It is version 3 of codeigniter
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Form_validation extends CI_Form_validation { protected $CI;
public function __construct() { $this->CI =& get_instance();
// Validation rules can be stored in a config file. $this->_config_rules = $rules;
// Automatically load the form helper $this->CI->load->helper('form');
// Set the character encoding in MB. if (function_exists('mb_internal_encoding')) { mb_internal_encoding($this->CI->config->item('charset')); }
log_message('debug', "Form Validation Class Initialized"); } function alpha_space($str) { $this->CI->form_validation->set_message('alpha_space', 'The %s may only contain Alpha Characters and Spaces.'); if (trim($str) !== "" & Is_a_Letter_or_Space_validation($str)) { return TRUE; } else { return FALSE; } } function Is_a_Letter_or_Space_validation($inputbox) {
$teststring = Trim($inputbox); if ($teststring == "") { return False;
}
$i = 0; while ($i <= strlen($teststring) - 1) { $c = strtolower(substr($teststring,$i,1));
if ($c <> "a" & $c <> "b" & $c <> "c" & $c <> "d" & $c <> "e" & $c <> "f" & $c <> "g" & $c <> "h" & $c <> "i" & $c <> "j" & $c <> "k" & $c <> "l" & $c <> "m" & $c <> "n" & $c <> "o" & $c <> "p" & $c <> "q" & $c <> "r" & $c <> "s" & $c <> "t" & $c <> "u" & $c <> "v" & $c <> "w" & $c <> "x" & $c <> "y" & $c <> "z" & $c <> " ") { return False; } $i = $i + 1; } return True; } }
The problem is in
Code: events: {
'click .submit-order-info': 'onSubmitClick'
},
initialize: function() {
},
el: '#order-info-form',
submit: function (callback) {
$.ajax({
url: DT_CONFIG.baseURL + 'ajax/submitorder', dataType: 'json', cache: false, timeout: 10000,
type: "POST",
data: this.$el.serialize(),
success: function (data) {
if (data.status === "success") {
callback(null, data);
} else {
callback(data.errors, data);
}
},
error: function () {
callback('GENERAL_ERROR');
}
});
},
onSubmitClick: function () {
var that = this;
this.$el.find(".form-errors").slideUp(500);
this.submit(function(err,data) {
if(err) {
var $formErrors = that.$el.find(".form-errors");
/* Errors found */
$formErrors.html("<h4>" + that.$el.find(".form-errors h4").text() + "</h4>" + err);
$formErrors.slideDown(500);
} else {
window.location = DT_CONFIG.baseURL + "step/shipping?purchase=" + data.orderData.order_id;
}
});
It is going into the else statement in onSubmitClick when there is an error
Thanks. I haven't added a new validater before so it is probably something obvious
I have used it several times before. You are right the regex would be better. It doesn't work even if I don't use the alpha_space rule.
|