Add form with validation using php and html to website
PHP Form Validation
I want to know what I should look for security wise when I validate form input with php.
Depends on what the input is.
Currently I am only using regular expressions to test input, is that enough?
No. Regex only helps you test for textual patterns, and certainly isn't enough if you're doing anything beyond that.
More on reddit.comCreating simple form validation and formatting elements in php?
How can i create a simple validation - just check if field isn't empty and how do i force user to type in numbers only
For simple validation I would rely on javascript/jquery/html to force your parameters
<input type="text" onKeyPress="if(this.value.length==6) return false;" id="number_id_field" required/>
if(this.value.length==6)
This only allows 6 digits in the text box, you can set it to whatever you like
This jquery function checks the key mapping on the key pressed and only allows numbers
//Function to only allow characters 0-9, if any other key is pressed, it will be deleted immediately. Works by
checking the key mapping of keyboard
$('#number_id_field').keyup(function (event) {
if (event.which !== 8 && event.which !== 0 && event.which < 48 || event.which > 57) {
$(this).val(function (index, value) {
return value.replace(/\D/g, "");
});
}
});required/
This makes the form input required so it cant be null
If you want to check that its numeric only using php, you can use a regular expression check
if (!preg_match('/^\d*$/', $number)) {
return json_encode("This is not a number");
}For removing whitespace at the beginning and end of strings, I would look into using ltrtim and rtrim
They are both functions for literally trimming whitespace at the beginning and end of strings respectively
More on reddit.com