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? Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/ 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. Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270423 Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 ;D You are zeh best, yet. Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270434 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? Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270450 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? Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270453 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. Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270463 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> Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270467 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; } ?> Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270469 Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 No indents = parse error? Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270474 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. Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270479 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 Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270480 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)) { Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270481 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. Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270486 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? Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270494 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... Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270503 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? Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-270515 Share on other sites More sharing options...
christofurr Posted June 8, 2007 Author Share Posted June 8, 2007 Problem still not solved. :-\ Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-271082 Share on other sites More sharing options...
christofurr Posted June 9, 2007 Author Share Posted June 9, 2007 :'( Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-271220 Share on other sites More sharing options...
christofurr Posted June 9, 2007 Author Share Posted June 9, 2007 Somebody assist, pl0x!@ Link to comment https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/#findComment-271253 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.