holy24 Posted July 29, 2013 Share Posted July 29, 2013 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; } Link to comment Share on other sites More sharing options...
birbal Posted July 29, 2013 Share Posted July 29, 2013 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 Link to comment Share on other sites More sharing options...
jeffman Posted July 29, 2013 Share Posted July 29, 2013 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now