Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Enter does not work for submitting password

I can't get the following code working: when I press enter in the text-box, the function is not called. I can't see why though...

<form>
<p align="center">
<input type="password" class="password" name="text1" onkeypress="submitonenter(text1.value,"money","cash",event)" /><br>
<input type="button" value="Enter" style="width: 100px" name="Enter" onclick=javascript:validate(text1.value,"money","cash") />
</p>
</form>

<script type="text/javascript">
    function submitonenter(text1,text2,text3,evt) {
        evt = (evt) ? evt : ((window.event) ? window.event : "")
        if (evt) {
            // process event here
            if ( evt.keyCode==13 || evt.which==13 ) {

 if (text1==text2)
 load('money/welcome.html');
 else 
 {
  if (text1==text3)
  load('cash/welcome.html');
   else
   {
   load('failure.html');
   }
 }

            }
        }
    }
</script>

<script language = "javascript">
function validate(text1,text2,text3)
{
 if (text1==text2)
 load('money/welcome.html');
 else 
 {
  if (text1==text3)
  load('cash/welcome.html');
   else
   {
   load('failure.html');
   }
 }
}
function load(url)
{
 location.href=url;
}
</script>
like image 647
Systech Avatar asked Apr 01 '26 00:04

Systech


2 Answers

I'm not sure why you need the submitOnEnter function at all.

Why not just change the input type='button' to type='submit' and change the onclick keyword to onsubmit?

EDIT: Apologies, of course the 'onsubmit' would need to be placed in the form tags, not the input. Giving the following:

<form onsubmit=validate(text1.value,"money","cash") >
  <p align="center">
    <input type="password" class="password" name="text1" /><br>
    <input type="submit" value="Enter" style="width: 100px" name="Enter" />
  </p>
</form>
like image 70
Jivings Avatar answered Apr 02 '26 13:04

Jivings


I would rewrite it all, and use a input type="submit" instead a button (I also changed the access to the password field, for being able to use it at Firefox):

<form id="myForm" method="POST" action="failure.html" onsubmit="return validate(document.getElementById('text1').value,'money','cash');">
<p align="center">
<input type="password" class="password" name="text1" id="text1"/><br>
<input type="submit" value="Enter" style="width: 100px" name="Enter" />
</p>
</form>

<script language = "javascript">
    function validate(text1,text2,text3) {
         var form=document.getElementById('myForm');
         if (text1==text2)
            form.action='money/welcome.html';
         else {
          if (text1==text3)
            form.action='cash/welcome.html';
           else {
              form.action='failure.html';
           }
         }
         return true;
    }
</script>

Edited: Implementing the onSubmit as recommended by @mway (thanks).

like image 22
Tomas Narros Avatar answered Apr 02 '26 12:04

Tomas Narros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!