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>
Check the input text's ascii to see if it is number or not!
or use is_numeric($var);
To check if something only contains digits, see the ctype_digit()
function.
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)">
You can use regular expression
if (preg_match('/^[0-9]+$/', $str)) {
} else {
}
精彩评论