Jump to content

Recommended Posts

hello again.

 

I am trying to ensure that when a visitor creates a user id that they include at least one number with the letters.

 

i have used eregi but it shows error as true if numbers are used at the begining of string. Obviously where the number id placed wont matter as long as at least one number is used.

 

if (ereg("[^[:alnum:]]",$userid)) $userid_error=true;

 

shows no error without number.

 

very stumped.

 

can you help?

 

thanks in advance

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/
Share on other sites

thanks, but i couldnt get it to work.

 

if (!eregi("^[a-z]+[0-9]+$",$userid)) $userid_error=true; is the closest ive got. however this does show an error when number is placed at begining of string. Ive tried without the begining and end string identifiers but that doesnt work at all.

 

 

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-735748
Share on other sites

I am not sure if there is a better way to do it, I am sure there is but this would work:

 

        preg_match('/(^[0-9]*)/i', $user, $matches);
if ($matches[1] != $user) 
	return array(false, "Username must contain at least 1 Numeric (123) character.");

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-735759
Share on other sites

thanks but again no luck. I did a copy and paste and then also tried altering but no luck. I seems that eregi should do it if only it would allow the number to be anywher on the string. but it only shows tru at beginning or end.

 

Its 1am here in UK i might need to sleep on this one lol

 

thanks for all your input  :D

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-735786
Share on other sites

shouldn't use ereg functions, as they will not be part of core build as of php6.

 

if (!preg_match('~^\w*[0-9]\w*$~',$string,$match)) {
   // incorrect format
}

 

note about that regex: you didn't really specify what else was acceptable, beyond requiring at least 1 digit to appear somewhere in the $string.  This pattern will match lowercase or uppercase letters, numbers or underscores (_).  Pattern will match 0 or more of those, and at least 1 digit.  In other words, if someone's name was "8" then it will return true.  You didn't specify beyond the 1 digit thing, so there you have it.

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-735812
Share on other sites

if (!preg_match('~^\w*[0-9]\w*$~',$string,$match)) {
   // incorrect format
}

 

Be careful with \w, as this includes an underscore (and I am assuming that the OP is looking only for letters (with at least one number)). Correct me if I'm wrong on that one though.

Also note that if just checking to see if a format is there or not, there is no need for the third argument within the preg parameters ($match).

 

Here is my [admittedly] bizarre take:

$str = array('passcode','passcode5','pass_8code','Passco9D4e','764243');
foreach($str as $val){
   echo (!preg_match('#^(?:[a-z][a-z0-9]*[0-9]+[a-z0-9]*|[0-9][a-z0-9]*[a-z]+[a-z0-9]*)$#i', $val))? $val.'= invalid<br />': $val.'= valid<br />';
}

 

Output:

passcode= invalid
passcode5= valid
pass_8code= invalid
Passco9D4e= valid
764243= invalid

 

The logic is using one massive do-not-capture alternation (I realise that in general, alternations are not preferable.. but they do have their occasional uses).

So the first part states: [a-z][a-z0-9]*[0-9]+[a-z0-9]*

If it starts with a letter [a-z], then it can comprise of letters and/or numbers afterwards [a-z0-9]*, but must contain at least one number [0-9]+ (assuming there might only be letters up to this point) then possibly more letters and/or numbers [a-z0-9]* afterwards, zero or more times...

 

The other half of the alternation is simply the inverse.. if it starts with a number, allow a letters and/or numbers, but must contain at least one letter (assuming there are only numbers up to that point) followed by zero or more letters and/or numbers..

 

Finally, not sure if upper and lowercase letters are permitted.. so I simply used the i modifier to allow both.. if this is not the case, simply remove the i, and change all the letters within the pattern to the desired case.

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-735863
Share on other sites

well if he doesn't want the _ then I would suggest

 

~^[a-z0-9]*[0-9][a-z0-9]*$~1

 

Your regex is a whole lot of overkill

 

The only reason for my overkill solution was to take into account that there must be letters as well as numbers (this may - or may not be an issue with the OP). As it stands, your solution permits only numbers. Again, if this is not an issue, then your solution is great.

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-735917
Share on other sites

hi everybody,

 

well the solution that seems to work is:

 

if (!eregi("^[a-z0-9]*[0-9][a-z0-9]*$",$userid)) $userid_error=true;

 

These brings up the error message if no number is included in the ID choice.

many thanks to crayon violent!!

 

many thanks everyone for all your help!  ;D

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-736414
Share on other sites

As does my code. In any case, as CV mentioned, you should stop using ereg. This is part of POSIX (Portable Operating System Interface -Unix) which is a more gerenalized, portable regex system which as of PHP 6 will no longer be inlcuded in the core. As a result, once your hosting provider switches over to PHP 6 (whenever it is released), and does not have the POSIX extension installed, your ereg code will not work.

 

Future proof your code by learning PCRE (Perl Compatible Regular Expressions). It is more robust than POSIX, and best of all, it is the favored version and quickly becoming the standard. This way, your code won't run the risk of breaking, even when PHP 6 arrives.

Link to comment
https://forums.phpfreaks.com/topic/140590-solved-user-id/#findComment-736579
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.