0

I'm trying to validate an input for Account number in php form. It should contain 8 numbers and '-' optionally. If there is '-' - it should be ignored. After pressing the Submit button, the warning message suppose to be displayed above the form in case input is invalid.

Please help.

This is what I got so far, but I'm not sure if this is correct and don't know how to display a warning message above the form.

$acctnum= "$acctnum";

if(empty($acctnum)){
  echo "You did not enter an account number, please re-enter"; }

else if(!preg_match("\-^[0-9]{8}", $acctnum)){
  echo "Your account number can only contain eight numbers. Please re-enter."; }

Thank you!

4
  • 2
    That's not a valid regex, you're missing delimiters. Commented Nov 18, 2012 at 20:55
  • Just one "-" or several of them? Commented Nov 18, 2012 at 21:05
  • Can you guarantee that all your accounts will have exactly eight digits in the future? Commented Nov 18, 2012 at 21:18
  • the condition of an assignment is that account number has to have exactly 8 digits Commented Nov 18, 2012 at 23:24

3 Answers 3

2

You don't appear to be trying. No documentation or tutorial will tell you to make a Regex like that. For starters, where are the delimiters? Why is - escaped when it's outside a character class and therefore has no special meaning? What is that ^ doing there?

This should do it:

$acctnum = str_replace("-","",$acctnum);
if( !preg_match("/^\d{8}$/",$acctnum)) echo "Error...";
Sign up to request clarification or add additional context in comments.

3 Comments

This will strip multiple -, not allow just one. Might be better to use an assertion here. But OP possibly just meant optional at the start(?)
Thank you! It should strip multiple '-'.
Kolink, thank you! I actually tried many other options. The one I wrote is just one of them. I didn't think about str_replace(), didn't use delimiters.
0

Since regex are quite expensive I'd go like that instead:

$acctnum = (int) $acctnum; // this automatically ignore the '-'
if ($acctnum < 0) $acctnum = -$acctnum;
$digits = ($acctnum == 0) ? log10($acctnum) + 1 : 1;
if ($digits === 8) { ... }

2 Comments

I don't think we will have performance issues with the regex for this simple task. And it would be more readable than your code. And what about leading zeros?
@yunzen, Since no one told me that there could be any and considering that $acctnum seems a counting integer this solution is faster. If it's not the OP solution at least I'll leave it here for future viewers.
0

Split the task in two. First get rid of the "-" with str_replace and then check for the numbers.

$match = preg_match("/^\d{8}$/", str_replace("_", "", $str));
if ($match > 0) {
    // Correct
} else {
    // incorrect
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.