0

Problem: When User enter "Space" in register form , it will still entered into database , how do i prevent it ?

Code I tried: Used trim , but same problem occurred when users only Enter "SPACE", how do i resolve the issue ?

$userName=  ($this->input->post("userName"))? trim($this->input->post("userName"), " ") : "NIL";
xaxacodess
  • 71
  • 8

2 Answers2

1

First of all your are assigning value to $username so it will not be null

You can check the value using

$userName= (trim($this->input->post("userName"), " ") !="")? trim($this->input->post("userName"), " ") : "NIL";

but this will assign value either ways, if it is null than $username will be NIL else the input value

Suggestion

 $username = "";
if(trim($this->input->post("userName"), " ") != ""){
    $userName= $this->input->post("userName");
    //insert into database
}else{
//redirect with error message
} 

Also, use client side and server side validation and check minimum length to that field

Regolith
  • 2,944
  • 9
  • 33
  • 50
1

$userName= ctype_space($this->input->post("userName"))? trim($this->input->post("userName"), " ") : "NIL";
 ----- OR ------
$userName= strlen(trim($this->input->post("userName")))!=0? trim($this->input->post("userName"), " ") : "NIL";

You can use ctype_space to check if the string contains only space . Please refer to this question If string only contains spaces?

Rahi
  • 324
  • 1
  • 11