bjenn85 Posted September 13, 2015 Share Posted September 13, 2015 I have a function I call to check if the title of a post is too long as a backup to maxlength on the form. I am now trying to add a simple check and stop the submission if it is in caps. I am just simply looking for the abscence of any lowercase letters, nothing complicated. Here is my snippet below but it's not working I always get the caps error even if the title isn't all caps. What am I doing wrong? Sorry I'm a little new to PHP. function checktitle($title){ if(strlen($title)>55) return getmsg('Title Is Too Long'); if(!preg_match('/[a-z]/',$title)); return getmsg('Title Is All Caps');} Quote Link to comment Share on other sites More sharing options...
boompa Posted September 13, 2015 Share Posted September 13, 2015 The ; at the end of this line if(!preg_match('/[a-z]/',$title)); means the if statement will have no effect, and the line immediately following will always be executed. Quote Link to comment Share on other sites More sharing options...
hansford Posted September 14, 2015 Share Posted September 14, 2015 (edited) My Opinions: When you bunch up your code like that it makes it difficult to follow and see where errors may be occurring. I would enclose conditional statements within braces. Less prone to errors as what happens if you decide to add an additional statement. If a function or variable name is more than one word long...each successive word should start with a capital letter or an underscore should be used between words for clarity. function checkTitle( $title ) { if(strlen($title) > 55) { return getMsg('Title Is Too Long'); } if(! preg_match('/[a-z]/', $title)) { return getMsg('Title Is All Caps'); } } Edited September 14, 2015 by hansford Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.