Jump to content

eregi help!


betamyprice.co.uk

Recommended Posts

Hi all, I am new to this board so firstly may I take the opportunity to say hi.

 

Here's my problem and I'm sure to you guys it is only a small one.

 

I am trying to validate entry into a form. I only want the first name to accept Alphabetic characters, along with apostrophes, hyphens and spaces.

 

From a bit of net research I currently have this:

 

if (!eregi("[_a-zA-Z0-9-]", $_POST[first] )) 
{
$_SESSION['errors'].= "*Your name must be alphabetic" . "<br>"; 
}

 

But it doesnt work, can someone help please  :)

Link to comment
Share on other sites

Try this out.

 

if (!preg_match("/[\w\s-]*/i", $_POST[first] )) 
{
$_SESSION['errors'].= "*Your name must be alphabetic" . "<br>"; 
}

 

I don't know if ereg will work but I'm pretty sure that preg will. Try it and come back to me.

 

Sorry about the error hope you get this edit please edit your reply if you do.

Link to comment
Share on other sites

if (!preg_match("/^[\w\s-]+$/i", $_POST[first] ))

 

See Character classes.

What exactly are the rules for apostrophes? How many can you have in an approved name..etc.. Where would it be allowed to be placed.. all things to think about.

Simply adding ' to the char class should do the trick, but it would also allow for ''''''''''''''''''''''''' to verify just like it already allows for ------------------- to verify.

<?php if (!preg_match("/^[\w\s-']+$/i", $_POST[first] )) ?>

 

Link to comment
Share on other sites

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.