0

I would like to compare user input with predefined value on same jsp page. If the input value is same as expected, then i allow user go ahead to submit the request form. But i don't know how set the predefined value for checking. Thanks.

<tr><td>Application</td></tr>

<% 
String strDBCode = "123456789";
out.println("<tr><td><b>Please input your pass code here:</b></td></tr>");
out.println("<tr><td><input type='text' name='strPassCode' value=''></td></tr>");
out.println("<tr><td><input type='hidden' name=strDBPassCode size='15' maxlength='15' value=strDBCode id='b'></td></tr>");
out.println("<tr><td><input type='button' name='chkPassCode' value=' Check ' onClick='return chkPCode()'></td></tr>");
%>

<script type="text/javascript">
function chkPCode()
{
    var a = document.getElementById("a");
    var b = document.getElementById("b");
    var valid = true;
    if (a.value !=  b.value) {
        alert("Not Match!");
        valid = false;
    }
    else {
        alert("Match");
        valid = true;
    }
    return valid;
}
</script>

I expect b value should be "123456789", but b always equal to "strDBPassCode". I have try set out.println(" id='b'>"); It return error. Since the input already inside <% %>.

Apart from that, how to disable the input text box if valid return is true?

Any idea? thanks.

stockjoe
  • 1
  • 1

2 Answers2

0

This is because you are not referring to the variable you declared.

out.println("<tr><td><input type='hidden' name=strDBPassCode size='15' maxlength='15' value="+strDBCode+" id='b'></td></tr>");
                                                                                       __________↑

Similar way, you can update the name attribute in above code.


Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code.
See How to avoid Java Code in JSP-Files?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • Thanks for help.It's work now. But now i am stick on another issue. I would like to compare user input and predefined value. Return true if user input like the predefined value. e.g. User input = "123", then return true. I have try use indexOf, match, includes function. But still not works. :( – stockjoe Aug 29 '19 at 07:44
0
<script type="text/javascript">
function chkPCode()
{ 
    var a = document.getElementById("a");
    var b = document.getElementById("b");
    var valid = true;
    if (a.indexOf(b) == -1) {
        alert("Not similar!");
        valid = false;
    }
    else {
        alert("Input value exist!");
        valid = true;
    }
    return valid;
}
</script>
stockjoe
  • 1
  • 1