Welcome Guest, Not a member yet? Register   Sign In
Form Validation alpha numeric dash "space"
#1

[eluser]wdcmatt[/eluser]
alpha numeric dash and SPACE!

I searched google for about 20 min and did not find a nice way to do this, i found a few poorly written one line scripts, still not to my liking, here is what I came up with hopefully it can help someone else...

Code:
/**
*    Method is used to validate strings to allow alpha
*    numeric spaces underscores and dashes ONLY.
*    @param $str    String    The item to be validated.
*    @return BOOLEAN   True if passed validation false if otherwise.
*/
function _alpha_dash_space($str_in = '')
{
    if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in))
    {
        $this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

And then I use this as a callback

Code:
$this->load->library('validation');
$rules['url_title']  = "required|callback__alpha_dash_space";
$this->validation->set_rules($rules);

Notice the double underscore after callback....

Well in any case I hope this helps someone.
#2

[eluser]Watnow[/eluser]
You can better extend you're form validation class en use this one:
Code:
function alpha_dash_space($str)
    {
        return ( ! preg_match("/^([-a-z0-9_ ])+$/i", $str)) ? FALSE : TRUE;
    }
with this textline in you're language file:
Code:
$lang['alpha_dash_space']    = "The %s field may only contain alpha-numeric characters, underscores, and dashes/spaces.";

I'v test it and its works.. and then you dont have to use it a callback function, just normal.
#3

[eluser]tomodatchi[/eluser]
just use jquery function like below in your views/page :

Code:
jQuery(function() {
    jQuery.validator.addMethod("alpha_dash", function(value, element) {
        return this.optional(element) || /^[a-z0-9_ \-]+$/i.test(value);
    }, "Alphanumerics, spaces, underscores & dashes only.");
    
    jQuery("#form_name").validate({
        "rules": {
            "firstname" : {"alpha_dash": true, "required": true},
            "phone"     : "required"
        }
    });    
});
#4

[eluser]WanWizard[/eluser]
Never rely on client-side validation! It can be easily worked around.
Always validate server-side!
#5

[eluser]tomodatchi[/eluser]
Better use both (client & server side validation)

The Cons of using one or the other :
With client side validation only, if a user disables JavaScript, then no validation will occur.
With server side validation only, there are more return trips to the server which in turns uses more resources and slows down the site when a client side script could have done the validation without the server.


The Pros of having both :
The data will always be validated, whether or not the client has JavaScript enabled or not.
There will be less server strain with the client side validation.
The client can fix form errors faster with instant client side notifications.
The data will have a bit more integrity going through the dual validation process.
#6

[eluser]Diggory Laycock[/eluser]
Thanks for this post - very useful.

I'd just like to point out something which caught me out:

If extending the form_validation class then you need to do a little more than is in the docs:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

   public function __construct($rules = array())
  {
   parent::__construct($rules);
   $this->CI->lang->load('MY_form_validation');
  }
   function alpha_dash_space($str)
  {
   return ( ! preg_match("/^([-a-z0-9_ ])+$/i", $str)) ? FALSE : TRUE;
  }

  }


?>


Note the $rules argument in the constructor.
#7

[eluser]TunaMaxx[/eluser]
[quote author="Diggory Laycock" date="1342528937"]If extending the form_validation class then you need to do a little more than is in the docs...
Note the $rules argument in the constructor.[/quote]
Do you have more information on this? I've never run in to the issue and I'd love to know what I am missing by not supplying the $rules argument.
#8

[eluser]Aken[/eluser]
The Loader class passes any config rules from config files through that param of the construct function. See /core/Loader.php :: _ci_load_class() and _ci_init_class().
#9

[eluser]TunaMaxx[/eluser]
Thank you. I did not know that!




Theme © iAndrew 2016 - Forum software by © MyBB