Javascript Form How to - Check Text input field box value before submitting the form








Question

We would like to know how to check Text input field box value before submitting the form.

Answer


 <!-- www.j a v  a  2s. c  o m-->
<HTML>
<HEAD>
<SCRIPT>
function checkLawyer(str) {  
   if ((str.toUpperCase() == "LAWYER") || (str.toUpperCase() == "ATTORNEY"))
      console.log ("Lawyers are not wanted here...");
   else
      console.log("Your application will be evaluated!");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm">
Enter Your Profession:
<INPUT type=text name="userProf">
<INPUT type=button 
       name="theButton" 
       value="SUBMIT QUERY" 
       onClick="checkLawyer(window.document.theForm.userProf.value)";>
</FORM>  
</BODY>
</HTML>

The code above is rendered as follows: