0

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>
1

4 Answers 4

3

Check the input text's ascii to see if it is number or not!

or use is_numeric($var);

Sign up to request clarification or add additional context in comments.

2 Comments

use echo not print, coz it's slightly faster as prints returns 0/1
@newbie just know that is_numeric() will return true for things that are not 0-9 ;; for example, is_numeric('-1.5e5') is true.
1

To check if something only contains digits, see the ctype_digit() function.

Comments

1

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)">

Comments

0

You can use regular expression

if (preg_match('/^[0-9]+$/', $str)) {

} else {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.