Jump to content

Regular Expressions


murfitUK

Recommended Posts

I'm trying to validate a username sent from a previous page using $_POST eg $name=$_POST['name'].Now, the name must be between 5 and 30 chars, letters, numbers and spaces ( ), hyphens (-), underscores (_), dots (.) and apostrophes (') only. Must start with a letter or number.So far, I've got if (eregi("^[a-z0-9][a-z0-9' _\.-]{4,29}$", $name))I've escaped the . to make it a literal character.This allows:user.nameuser-nameuser nameuser_nameuser'nameusernameetcHowever, it also returns true for user\name which I don't want. But if I take out the \ from the expression it starts allowing all sorts of characters.What am I doing wrong?

Link to comment
Share on other sites

I think the dot loses its special meaning inside brackets... What extra characters does it allow?I've isolated the regex with the URL as the input mechanism, in case it comes in handy...

<?php$name = $_GET['name'];echo $name . "\n";echo eregi("^[a-z0-9][a-z0-9' _.-]{4,29}$", $name) . "\n";?>

Link to comment
Share on other sites

Though ranges are nice, how about letting go of this one, and use a dot outside of it in a group, like so:

if (eregi("^[a-z0-9]([a-z0-9' _-]|\.){4,29}$", $name))

This should work, though I'm not sure whether it's as efficient as a range would go, and it's certainly not neat.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...