Jump to content
Sign in to follow this  
holy24

If else statement

Recommended Posts

Hi,

 

I have created a form for user to fill and I need to ensure that some fields are filled in with the necessary requirements.

 

Example: Identification number (9 alphabet/numberic) - S1234567A

 

Can someone kindly advice how can I ensure that only 9 alphabet/numberic is keyed by user?

 

I understand that using sql statement there is a WHERE clause with Like operator (identification LIKE '_________')

 

Can it be done using if else statement?

 

if (identification LIKE '_________'){

alert("You have not entered correct Identification number");

return false;

}

Share this post


Link to post
Share on other sites

you can use http://php.net/preg_match to match particular pattern. IKE is SQL operator and has different purpose. what you are asking is callled validation. validation whould be done before you make DB queries. check like operator here http:///w3schools.com/sql/operator_like.asp

Share this post


Link to post
Share on other sites

Birbal is correct. Since regular expressions are not easy to learn, I have written one that will do your job. I suggest you study regular expressions if you need to validate a different kind of string.

 

 

function validates ($str) {    return preg_match("/^[a-z0-9]{9}$/i", $str);}// USE IT LIKE THIS:$s = "abcd12456";if (validates($s) ) {    echo "Yes";} else {    echo "No";}

 

/^[a-z0-9]{9}$/i is the regular expression. It makes sure that all the characters are letters [a-z] or digits [0-9]. It makes sure the string is 9 characters long {9}. Since a 9-character string can be part of a 10-character string, the expression makes sure that the 9 characters fall between the exact ^beginning and the exact end$ of the string. The i at the end allows the letters to be uppercase or lowercase.

  • Like 1

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...