This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

regular expressions


  1. arnaud404
    Member

    How deep I have to go to change the "required" option by a "use a regex" which will bring me a lot more into the validation & input validation ?
    (for example, for the number field, use "\d{4}" instead of the basic "required" option)

    this is the same post as my prévious one called "regex?", but the forum seems to be broken with that keyword in the title (sorry)

    Posted 16 years ago #
  2. arnaud404
    Member

    I have also noticed that a simple 'espace' broke the "required" validation of a field

    Posted 16 years ago #
  3. yuniar

    Go to includes/common-validator.php file and adjust the validate_required() function to use your regex.

    Few functions there, such as validate_email() and validate_phone() are using regex to do validation, you might want to take a look at those for samples plugging in a regex.

    Regarding the space, a space is considered an input too. I decided to let it go instead of trimming it.


    MachForm Founder

    Posted 16 years ago #
  4. dgrebb
    Member

    I cannot figure out how to create an additional validator.

    In includes/common-validator.php I added the following code:

    //validation to disallow any common html
    	function validate_fullname($value){
    
    		$error_message = VAL_FULLNAME;
    
    		if(preg_match('\<|\>|www|\.',$value[0])){
    			return sprintf($error_message,'%s',$value[0]);
    		}else{
    			return true;
    		}
    	}

    directly below this code:

    //validation to allow only a-z 0-9 and underscores
    	function validate_username($value){
    
    		$error_message = VAL_USERNAME;
    
    		if(!preg_match("/^[a-z0-9][\w]*$/i",$value[0])){
    			return sprintf($error_message,'%s',$value[0]);
    		}else{
    			return true;
    		}
    	}

    In /includes/post-functions.php, I added:

    $rules[$element_name]['fullname'] = true;
    $rules[$element_name_2]['fullname'] = true;

    directly below

    }elseif ('simple_name' == $element_type){ //Simple Name
    
    	if(is_array($processed_elements) && in_array($element_name,$processed_elements)){
    		continue;
    	}
    
    	//compound element, grab the other element, 2 elements total
    	$element_name_2 = substr($element_name,0,-1).'2';
    
    	$processed_elements[] = $element_name_2; //put this element into array so that it won't be processed on next loop
    
    	if($element_info[$element_id]['is_required']){
    		$rules[$element_name]['required'] 	= true;
    		$rules[$element_name_2]['required'] = true;
    	}

    and also added:

    $rules[$element_name_2]['fullname'] = true;
    	$rules[$element_name_3]['fullname'] = true;

    directly below:

    }elseif ('name' == $element_type){ //Name -  Extended
    
    	if(is_array($processed_elements) && in_array($element_name,$processed_elements)){
    		continue;
    	}
    
    	//compound element, grab the other element, 4 elements total
    	//only element no 2&3 matters (first and last name)
    	$element_name_2 = substr($element_name,0,-1).'2';
    	$element_name_3 = substr($element_name,0,-1).'3';
    	$element_name_4 = substr($element_name,0,-1).'4';
    
    	$processed_elements[] = $element_name_2; //put this element into array so that it won't be processed next
    	$processed_elements[] = $element_name_3;
    	$processed_elements[] = $element_name_4;
    
    	if($element_info[$element_id]['is_required']){
    		$rules[$element_name_2]['required'] = true;
    		$rules[$element_name_3]['required'] = true;
    	}

    What am I missing? Any help is much appreciated :)

    Posted 16 years ago #
  5. yuniar

    Hmm..it seem you have put it in the right places.
    However, what is this regex do?

    preg_match('\<|\>|www|\.',$value[0])

    I tested with a simple code:

    $value[0] = "<b>test</b>";
    
    preg_match('\<|\>|www|\.',$value[0],$matches);
    print_r($matches);


    and I get "Warning: Delimiter must not be alphanumeric or backslash"
    I think there is some glitch with the regex


    MachForm Founder

    Posted 16 years ago #
  6. dgrebb
    Member

    The regex was indeed flawed, however the PHP still seems to fail.

    Here is my new regex: (\<(/?[^\>]+)\>)

    I am attempting check a field for any HTML characters, and return the $errormessage I've added: define('VAL_FULLNAME','This field may only consist of a-z 0-9 and spaces.');

    Thanks so much for the quick response :)

    Posted 16 years ago #
  7. yuniar

    I can't seem to make your new regex working either. Let's try using this alternative regex:

    //validation to disallow any common html
    function validate_fullname($value){
    
    	$error_message = VAL_FULLNAME;
    	if(!preg_match("/^[a-zA-Z0-9 ]*$/",$value[0])){
    		return sprintf($error_message,'%s',$value[0]);
    	}else{
    		return true;
    	}
    }


    If you still having trouble, I'll mail you the code.

    Also, the modification in post-functions.php file, make sure you modify the right code inside process_form() function, not inside process_form_update().

    Those two function are similar.


    MachForm Founder

    Posted 16 years ago #
  8. dgrebb
    Member

    Thanks so much yuniar. The problem was indeed a bad regex, and I was editing the wrong code, inside process_form_update(), instead of the code inside process_form().

    Again I commend your work on this project. It will make life much easier on my end.

    Cheers!

    Posted 16 years ago #
  9. yuniar

    Sounds great :-)


    MachForm Founder

    Posted 16 years ago #
  10. rafalsak
    Member

    Hi,
    I've a problem with my new validation. Here is what I've done:

    In the file: common_validator.php, I included the code:

    function mf_validate_indeksy($value){
    		global $mf_lang;
    
    		$error_message = $mf_lang['val_indeksy'];
    
    		if(!empty($value[0])){
    			$regex  = '/^([0-9]*[\n]?)*$/';
    			$result = preg_match($regex, $value[0]);
    
    			if(empty($result)){
    				return sprintf($error_message,'%s',$value[0]);
    			}else{
    				return true;
    			}
    		}else{
    			return true;
    		}
    	}

    In the file: post-functions.php, I included the code:

    if ($form_id == 4 && $element_id == 17){
    					$rules[$element_name]['indeksy'] = true;
    				}

    after the code:

    elseif ('textarea' == $element_type){ //Paragraph
    
    				if($element_info[$element_id]['is_required']){
    					$rules[$element_name]['required'] 	= true;
    				}

    I expected the result: the filed of type 'Paragaph' and the id = 17 included in the form_id =4,
    should only accept digits and crl\lf (break of line). I mean there is only allowed list of numbers.

    After this changes, there is no result I expected. Please help me ...

    Posted 10 years ago #

RSS feed for this topic

Reply