MultiLine Regex Element Validation

nbradshaw

Active Member
I am trying to validate lat,long in a text field and I need regex validation.

Text Input looks like:
32.438667,-85.844944
32.1960111,-86.4252639
32.438667,-85.844944

I am using (?s)(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?[\r\n\s]*)

DEMO: https://regex101.com/r/lC3iO5/8

If I only enter 1 line of lat,long values - then the validation is picked up and an error is thrown...however, multi lines are not picked up.

Will it support multiLine?

Thanks -
 
Meh, it might be possible, by appending options like /g or /m, but that's getting into Ninja level regex. The problem being I think it's still going to return true as just one of the lines matches. So you'd have to do some kind of funky inverted test for NOT matching NOT what you want.

Easier to use a PHP validation plugin, bust the input up into an array of lines, and just do a simple single line regex on each:

Code:
// explode the string into an array by line breaks, using preg_split and all possible EOL combinations for UNIX and Windows
$inputs = preg_split("/\r\n|\r|\n/", $data);
// loop around ...
foreach($inputs as $input) {
   // if it doesn't match, bail out with false
   if (!preg_match('', $input)) {
      return false;
   }
}
// if we got this far, al ist in ordnung, return true
return true;

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top