David Kvindesland
David Kvindesland

Reputation: 23

PHP input form with exact amount digits

i am making a database to contain the names and information about all my book's.

I want to include the ISBN number in my input field and am wondering how to only accept a number with a 13 digits.

right now i am using this:

<form action="insert.php" method="post">
<li>ISBN:* <input type="number" name="isbn">

Upvotes: 0

Views: 555

Answers (2)

Flixer
Flixer

Reputation: 958

you can do it both, on client side and on server side. Client side is optionally, server side is required.

Client side

<input name="isbn" type="number" minlength="13" maxlength="13">

Server side

if (strlen($_POST['isbn']) == 13 && preg_match('/^\d+$/',$_POST['isbn'])){
    // isbn is valid
}

Upvotes: 0

Jake N
Jake N

Reputation: 10583

// Check the form was submitted
if(!empty($_POST))
{
  // Simple validation check that the length is 13 and that there are only numbers
  if(strlen($_POST['isbn']) != 13 || !preg_match("/^[0-9]*$/", $_POST['isbn']))
    echo "ISBN needs to be 13 digits in length";
  else
    echo "ISBN is valid";
}

Upvotes: 1

Related Questions