Reputation: 14950
Good day!
How can I produce an error if i input a letter instead of number because characters can still be encoded.... My code is as follows:
<HTML>
<BODY>
Please enter your grade (0-100 only):
<FORM ACTION="grade.php" METHOD="POST">
<table border = "1">
<tr>
<td>Grade</td>
<td><input type="text" name="grade" /></td>
</tr>
</table>
<INPUT type="submit" name="submit" value="grade status">
</FORM>
<?php
$grade = $_POST['grade'];
IF ($grade>=0 && $grade<=50){
print "You failed";
} ELSE IF ($grade<=60){
print "You Passed! But Study Harder!";
} ELSE IF ($grade<=70){
print "Nice";
} ELSE IF ($grade<=80) {
print "Great Job";
} ELSE IF ($grade<=90){
print "Excellent";
} ELSE IF ($grade<=100){
print "God Like!";
} ELSE {
print "Invalid Grade!";
}
?>
</BODY>
</HTML>
Upvotes: 0
Views: 284
Reputation: 17520
Check the input text's ascii to see if it is number or not!
or use is_numeric($var);
Upvotes: 3
Reputation: 742
You can use regular expression
if (preg_match('/^[0-9]+$/', $str)) {
} else {
}
Upvotes: 0
Reputation: 787
Use javascript:
<script language="javascript">
function isInteger(s)
{ var i=document.s.grad.value;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9")))
{
alert("Must Enter Number");
return false;
}
}
// All characters are numbers.
return true;
}
</script>
and call this function in form tab
<FORM ACTION="grade.php" METHOD="POST" onSubmint="isInteger(this)">
Upvotes: 1
Reputation: 401032
To check if something only contains digits, see the ctype_digit()
function.
Upvotes: 1