473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Validate User Input

Hey all!

I am making a very simple program which requires the user to enter a numerical value into a text box (html form) and then multiplies it by 10 before echoing back. The program is working fine.

However, when I enter some letters, php returns an ugly error. Is there a way to prevent the user from entering letters?

Thanks!
Dec 22 '07 #1
1 1464
Markus
6,050 Expert 4TB
This was a little learning curve for me. So thanks for letting me help you :D!

I'll start with the code:
[php]
<?php

$stringGood = "2345634545156456";
$stringBad = "32873428473t30984";

$pattern = '/[^0-9]/'; //pattern for regular expression
if(preg_match($pattern, $stringBad)){
echo "String: $stringBad, contained characters <em>other than</em> numbers.";
} else {
echo "String was good.";
}

echo "<br /><br />

if(preg_match($pattern, $stringGood)){
echo "String: <strong>$stringGood</strong>, contained characters <em>other than</em> numbers.";
} else {
echo "String was good.";
}

?>
[/php]
You better read this!
First we start off with the strings we are going to check ($stringGood, $stringBad) named like so for obvious reasons ;) - i assume you'll need to change the variables to something like:
[php]
$string = $_POST['number'];
[/php]
The next thing ($patter) is the pattern we will use for the regular expression, and don't be worried if these scare you - they scare me! But once you try it for yourself they aren't too ghastly.
Basically what the pattern is saying is:
[start pattern ^check for something that is NOT 0-9included in this part ]end pattern.
So we're checking for anything that is not within the scope of 0, 1, 2...
Easy eh?
Next we're onto the preg_match. What this does is check the given subject for any given regular expression. Great. Exactly what we need.
If the given expression is found in the given subject; it will return 1 / true. And i bet you can tell me what it returns if it isn't found? ;)
So so:
[php]
if(preg_match returns TRUE){
//pattern was found - execute code.
} else {
//pattern not found - execute code.
}
[/php]

Hope this long winded solution helps!

Any questions just post back :)

markus.
Dec 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mats | last post by:
Hell My problem is that I do not know how to validate input that I let the user type into my datagrid In one grid I let the user to type in a value into a template column - how do I make sure...
5
by: sjl | last post by:
I've got an .aspx webform for searching my database. It basically takes user input and passes it as a parm into a stored proc to search a table. The results are returned in a SQLDataReader and...
0
by: Marina | last post by:
Let's say a user control has a bunch of textboxes. At some point the user control calls Validate to make sure all input is processed. The Validating event is fired, and if the input was invalid,...
4
by: Mike Fellows | last post by:
running IIS on a single server, hosting pages on an intranet basis, one single user out of 50 is having an unable to validate data issue how do i fix this (the microsoft KB is a little...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
6
by: Ed Jay | last post by:
When I try to validate the following I receive three errors: Error Line 47 column 41: document type does not allow element "INPUT" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6",...
5
nathj
by: nathj | last post by:
Hi, I have been looking around the forum and the web for a way to achieve this and so far I have drawn a blank. So I head to the forum as I'm sure someone knows how to do this. I have a form...
3
by: Max2006 | last post by:
Hi, I have a user control that has a property named "Text" Is there any way that I use a RequiredFieldValidator or CompareValidator to validate my user control's value? I tried it and I...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.