christofurr Posted June 8, 2007 Share Posted June 8, 2007 I suck at writing PHP. I'm trying to create a basic registration form, but I'm having trouble understanding how to allow and disallow specific characters in an text field; I want the Full Name field to allow only A-Z, a-z and spaces; I want the Username field to allow only A-Z, a-z, 0-9, and spaces; I want the Password field to allow only a-z and 0-9. Can anyone explain in elemenary english how this can be accomplished?? P.S. - Did I mention that I suck at writing PHP? Quote Link to comment Share on other sites More sharing options...
Dragen Posted June 8, 2007 Share Posted June 8, 2007 ereg if(!ereg('^([A-Za-z]+[:space:]*)+$', $string){ //throw up an error }else{ //name is correct } that will only allow letters and spaces in an input. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 ;D You are zeh best, yet. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 After reading a bit about regular expressions, this is what I've come up with: if (!ereg('([a-zA-Z0-9]+[:space:]*){3,32}', $string)) { echo "Invalid characters in Username field."; } elseif (ereg('^[:space:]+', $string)) { echo "Username cannot begin with a space."; } elseif (ereg('[:space:]+&', $string)) { echo "Username cannot end with a space."; } else { echo "Your Username is " . $string; } If I wrote it correctly, it should display "Invalid characters in Username field" if there are any characters that aren't alphanumeric or spaces in $string, or if there are fewer than 3 or more than 32 characters in total. It should also display "Username cannot begin with a space" if $string starts with one or more spaces or "Username cannot end with a space" if $string ends with one or more spaces. Did I make any errors? Quote Link to comment Share on other sites More sharing options...
trq Posted June 8, 2007 Share Posted June 8, 2007 Did I make any errors? Does it work? Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Not sure... Hadn't uploaded it. I'll check it now. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Mmm... Parse error. Can't find my mistake. <html> <body> <p> <form action="form4.php" method="post"> Full Name: <input type="text" name="name" value="<?php echo $_POST["name"]; ?>" size="35" maxlength="35" /> Email Address: <input type="text" name="email" value="<?php echo $_POST["email"]; ?>" size="35" maxlength="50" /> <input type="submit" value="Create Account" /> </form> </p> <p> Your name is <?php echo $_POST["name"]; ?>. <br /> Your email address is <?php echo $_POST["email"]; ?>. <br /> </p> <p> <?php if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL) { echo "Invalid email address.<br />"; } if (!ereg('([a-zA-Z0-9]+[:space:]*){3,32}', $_POST["name"])) { echo "Invalid characters in Full Name field."; } elseif (ereg('^[:space:]+', $_POST["name"])) { echo "Full Name cannot begin with a space."; } elseif (ereg('[:space:]+&', $_POST["name"])) { echo "Full Name cannot end with a space."; } else { echo "Your Full Name is " . $_POST["name"]; } ?> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
trq Posted June 8, 2007 Share Posted June 8, 2007 It helps if you indent your code so its readable. <?php if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL) { echo "Invalid email address.<br />"; } elseif (!ereg('([a-zA-Z0-9]+[:space:]*){3,32}', $_POST["name"])) { echo "Invalid characters in Full Name field."; } elseif (ereg('^[:space:]+', $_POST["name"])) { echo "Full Name cannot begin with a space."; } elseif (ereg('[:space:]+&', $_POST["name"])) { echo "Full Name cannot end with a space."; } else { echo "Your Full Name is " . $string; } ?> Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 No indents = parse error? Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 It tells me that there's an unexpected '{' on line 25, but I don't see an extra brace. Quote Link to comment Share on other sites More sharing options...
jaikar Posted June 8, 2007 Share Posted June 8, 2007 even a correct code in one big line will not give a parse error, indents helps to read the code and understand easily..... the extra brace need not be on that line, some brace would have added or moved before that.... let me check that too.. ~J Quote Link to comment Share on other sites More sharing options...
jaikar Posted June 8, 2007 Share Posted June 8, 2007 change this line if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL) { to if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)) { Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Good eye, Jaikar. As for my sloppy coding, hopefully Dreamweaver can take care of that for me. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Call to undefined function: filter_input() ??? Why be it undefined? Quote Link to comment Share on other sites More sharing options...
jaikar Posted June 8, 2007 Share Posted June 8, 2007 filter_input() is a function and it should exit in the page, or the page that has this function should be included... Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Exit in the page? How do you mean? Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Problem still not solved. :-\ Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 9, 2007 Author Share Posted June 9, 2007 :'( Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 9, 2007 Author Share Posted June 9, 2007 Somebody assist, pl0x!@ 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.