Jump to content

Recommended Posts

Hi Guys

 

Quick question. I am trying to write a sort of multi purpose validation function (just for simple forms).

 

I want to pass a dynamic value to the length part of the regular expression patterns below. I can get this to work for the 'tstring' case in the switch statement (i.e. where i pre-concatenate the values). But if i use complex syntax and then try to pass the values separately ( as in 'tint'  case below) i can't seem to get it to work. Is it possible to do? Or do i need to pre-concatenate first?

 

Thanks chaps!

 

function call looks like:

$validate->multiVal($_POST['name'], 1, 6, 'tstring');

 

Actual function:

public function multiVal($subject, $lenMin, $lenMax, $type){

	$length = $lenMin . "," . $lenMax;
	switch($type){

		case 'tstring':
		$pattern = "/^[a-zA-Z\s\b]{{$length}}$/";
		break;

		case 'tint':
		$pattern = "/^[0-9]{{$lenMin}, {$lenMax}}$/";
		break;


	}


	preg_match($pattern, $subject, $matches);
                 // check results and return true or false
	}

Link to comment
https://forums.phpfreaks.com/topic/267313-complex-syntax-and-preg_match/
Share on other sites

What does the finished pattern look like when you test it?

As far as I can tell from what you've posted, it should work. Though it might be the space after the comma that creates problems for you. Not sure, as I've never tried with a space there.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.