Ghamdan Posted October 12, 2012 Share Posted October 12, 2012 Chapter 18 - registration form. I am working on chapter 18 registration form when I validate the name with reg exp it does not accept the input value and reset works fine. php code: // Validate name: if(preg_match( '/^[A-Z \'.-]{2, 40}$/i' , $trimmed['name'])) { $p_name = mysqli_real_escape_string($dbc , $trimmed['name'] ); } else { echo '<p class="error"> Please enter your name. </p>'; } html code: <label for="name" > Name: </label> <input type="text" id="name" name="name" size="30" maxlength="40" value="<?php if(isset($trimmed['name'])) echo $trimmed['name'] ; ?>" /> How can I solve this issue? Link to comment Share on other sites More sharing options...
HartleySan Posted October 12, 2012 Share Posted October 12, 2012 What do you mean, "It doesn't accept the input value?" 1 Link to comment Share on other sites More sharing options...
Ghamdan Posted October 13, 2012 Author Share Posted October 13, 2012 I mean when I use this RegExp '/^[A-Z \'.-]{2, 40}$/i' then I enter a name, it does not accept the entered name. Therefore it prints the error message: echo '<p class="error"> Please enter your name. </p>. Thank you Link to comment Share on other sites More sharing options...
Dan Posted October 13, 2012 Share Posted October 13, 2012 Where is your code in which you define the $trimmed array? Link to comment Share on other sites More sharing options...
Ghamdan Posted October 13, 2012 Author Share Posted October 13, 2012 This is the code which I have defined the $trimmed array: // Trim all the incoming data: $trimmed = array_map('trim', $_POST); Thank you Link to comment Share on other sites More sharing options...
Dan Posted October 13, 2012 Share Posted October 13, 2012 In your HTML code, try changing the label for="name" to something other than "name". I cut and pasted your if statement into my register.php script and that worked fine and the $trimmed statement also looks ok....that leaves the HTML. Link to comment Share on other sites More sharing options...
Dan Posted October 13, 2012 Share Posted October 13, 2012 On second look..my suggestion is probably wrong. I just found some HTML code that does the same thing with the label for tag. I still think it may be in your html though. Your if statement worked for me with my HTML which matches what is in the book. Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 13, 2012 Share Posted October 13, 2012 A-z is often a too small range. What about Oddbjørn, Åsmund or Æinar? You can't easily make sure a name is valid. I would ensure min and Max length, but leave the rest to the user. Unless not critical for your app to work, you should sanatize input more than you validate it. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 13, 2012 Author Share Posted October 13, 2012 Now I am using the filter_var() to validate name as you can see below. // Validate name: if(filter_var( $trimmed['name'], FILTER_SANITIZE_STRING)) { $p_name = mysqli_real_escape_string($dbc , $trimmed['name'] ); } else { echo '<p class="error"> Please enter your name. </p>'; } Is that right? Thank you Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 13, 2012 Share Posted October 13, 2012 Very good. That is better. You could also look at trim() and strlen() too. Maybe even use a combo of ucword(strtolower()); to make sure of case, etc. Link to comment Share on other sites More sharing options...
Ghamdan Posted October 13, 2012 Author Share Posted October 13, 2012 Thank you for helping me. Link to comment Share on other sites More sharing options...
Recommended Posts