how to esacpe single quotes for codeigniter form_validation - php

I am trying to show validation error if single quotes are passed with the below function, but its not working out
function alpha_dash_space($str) {
return (!preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
}
-
$this->form_validation->set_error_delimiters('<li class="errorlist">', '</li>')->set_rules('book_title', 'Book Title', 'trim|required|min_length[2]|max_length[150]|xss_clean|callback_alpha_dash_space');
Example:
If input provided for book_title = The Roa'r of King's
Expected validation error message should
show "Book Title has invalid character"
if "The Roar of Kings" of passed then there will be no validation error.

If you're validating form input rather use CodeIgniters own implementation. Your form validation rules should look like the below.
$this->form_validation->set_rules('username', 'Username', 'alpha_dash');

try this one
function custom_alpha_dash_spaces($str) {
if (!preg_match("/^([-a-z0-9_ -])+$/i", $str)) {
$this->form_validation->set_message('custom_alpha_dash_spaces', 'The %s can not contain quotes.');
return FALSE;
}
return true;
}

Related

return message if input is in array

I have a form validation that, so far, returns an error message if either of two defined words/phrase are present in the input area:
add_filter('gform_validation_3', 'custom_validation');
function custom_validation($validation_result){
$form = $validation_result["form"];
foreach($form['fields'] as &$field){
/* Check that the value of the field that was submitted. e.g. the name="input_1" that is generated by Gravity Forms */
if($_POST['input_4'] == "Your First Name" || "SEO"){
// set the form validation to false
$validation_result["is_valid"] = false;
//The field ID can be found by hovering over the field in the backend of WordPress
if($field["id"] == "4"){
$field["failed_validation"] = true;
$field["validation_message"] = "This field needs to be your actual first name.";
}
}
}
//Assign modified $form object back to the validation result
$validation_result["form"] = $form;
return $validation_result;
}
I'm not sure now how to create an array to define the words that are not allowed, so that I can have a much longer list?
First of all, the first "if" is incorrect, I think you meant:
if($_POST['input_4'] == "Your First Name" || $_POST['input_4'] =="SEO")
A good way to achieve what you long is:
$forbidden_words = ["Your First Name", "SEO"];
$is_valid = !in_array($_POST['input_4'], $forbidden_words); //false if the word is in array
After that you may go:
if($is_valid)
//do magic
You can use function in_array()
<?php
$blacklisted = ['some', 'ugly', 'bad', 'words'];
if(in_array('ugly', $blacklisted)){
echo('bad word spotted');
}
demo: https://repl.it/#kallefrombosnia/DarkvioletDeepPolygons

form validation with preg_match in callback not working

I have questions about my codeigniter form validation. I try to validate the input form for name so it will generate error if user using symbol like ">?<*&%^$". Here is my code:
My rules:
$this->load->library('form_validation');
$this->form_validation->set_rules('full_name', 'Name', 'trim|required|callback_name_check',
array(
'name_check' => '%s should not using symbols'
)
);
This is my callback function (I tried to modify this from the last example I saw, so I thought the problem was here)
public function password_check($str)
{
if (preg_match('#[<>?&%$##]#', $str)) {
return TRUE;
}
return FALSE;
}
I have tried another example from another StackOverflow answer to use / as delimiter (like this --> [/<>?&%$##/]), but still, doesn't work. I'll appreciate your help sensei :)
Validation should be
$this->load->library('form_validation');
$this->form_validation->set_rules('full_name', 'Name', 'trim|required|callback_name_check');
Inside call back function
public function name_check($str)
{
if (preg_match('#[<>?&%$##]#', $str)) {
{
return TRUE;
}
else
{
#adding new validation error should be
$this->form_validation->set_message('full_name', '%s should not using symbols'); # input field name should come to first
return FALSE;
}
}
Note: Didn't validate REGEX which you have posted

Password cannot be Username in CodeIgniter form validation

On registration, I want my 'password' field to have the following custom rule:
not_matches[username]
I would then set the following language:
$lang['not_matches'] = "The %s field cannot be the same as the %f field";
(Assuming %f is the field name)
Is this possible?
To be clear, I know how to do not_matches[".$val."] but I would like a flexible rule instead.
Im not sure that i understand what u are saying. You want a rule that says not_matches['username'] being the username another input field?
If so, just go to system\libraries\form_validation.php , then find the matches rule and duplicate it changing the == to !== and the name to not_matches. Then go to system\language\english\form_validation_lang.php and create the message.
There is no such support yet in CI. You need to write your own callback validation routine.
There is an example that can be easily adapted to your needs in the CI manual.
Here's how I did it.
application/language/english/form_validation_lang.php
$this_lang = basename(__DIR__);
require_once("./system/language/$this_lang/form_validation_lang.php");
$lang['not_match'] = "The %s field must not match the %s field.";
application/libraries/MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation {
function __construct()
{
parent::__construct();
}
public function not_match($str, $field)
{
if ( ! isset($_POST[$field]))
{
return FALSE;
}
$field = $_POST[$field];
return ($str === $field) ? FALSE : TRUE;
}
}
Then the rule is simply not_match[field_name]

Codeigniter use form validation for variable

I've read somewhere that is possible to use Codeigniter's Form Validation also for my own variables and not only for form's inputs.
For example I'd like to validate a url to say if it is valid but not retrieving it using POST or GET from a form.
Do you know how or have a link?
What you are looking for are the callbacks of the Form Validation Class in CodeIgniter - read the user guide for an in-depth explanation.
For PHP5 above version,you can do this
function validdate_urls($str) {
if(!filter_var($str, FILTER_VALIDATE_URL))
{
$this->validation->set_message('validate_urls', 'URL Invalid');
return 0;
}else {
return TRUE;
}
}
And call it in your validation rules :-
$rules['link'] = "callback_validate_urls";
Yes you can via set_data() method, Here you go.
$this->form_validation->set_data(array(
'cartId' => $cartId
));
$this->form_validation->set_rules('cartId', 'Card ID', 'trim|required|is_natural_no_zero');
if ($this->form_validation->run() == FALSE) {
echo 'Invalid: ' . validation_errors();
} else {
echo 'Valid';
}

How to set custom error message with form_validation And CodeIgniter

i am newbie in CodeIgniter...and i am trying to do form validation for array input...
the array name is pages[].
and i wrote:
$this->form_validation->set_rules('pages[]','','required');
if i use that:
$this->form_validation->set_message('required', 'you not selected pages.');
it will not change the other "required" validation input params?
So how can i set error message only for one validation?
This is my custom Form_Validation class. you can use it if you want to. put this file under your libraries directory. then you can use the set message like this:
$this->form_validation->setError(YOUR_INPUT_NAME, THE_MESSAGE);
ex: $this->form_validation->setError('email', 'Invalid email');
--
class MY_Form_validation extends CI_Form_validation {
public function set_error($field, $pesan_error){
$this->_field_data[$field]['error'] = $pesan_error;
}
public function get_error($field){
return $this->_field_data[$field]["error"];
}
public function get_all_error(){
// return $this->_field_data[$field]["error"];
$fields = $this->_field_data;
$pesan = "";
foreach($fields as $field ) {
if($field["error"]) {
$pesan .= "<p>$field[error]</p>";
}
}
return $pesan;
}
}
It doesn't work like you stated, you should read this section of the user guide more carefully.
I'm not sure I can explain better, but the first field of the set_message method doesn't refer to the type of validation but to the callback function's name, that's the function which is doing the custom validation work.
What you need to do is define your callback function (the guide has a good example), in which you iterate through your array's elements and count what's checked. If at the end of the iteration the counter is 0 you set your error message.
Hope this helps.

Categories