<form action="receive.php" method="post">
<input type="text" name="fname" placeholder="first name" autocomplete="off" onkeydown="javascript: return random_function(event)">
<input type="text" name="lname" placeholder="first name" autocomplete="off" onkeydown="javascript: return random_function(event)">
<input type="submit" name="" value="submit">
</form>
<script type="text/javascript">
function random_function(evt)
{
var bool=true;
if(evt.keyCode==13)
{
bool=false;
}
return bool;
}
</script>
receive.php
<?php
echo "first name is ".$_POST['fname']. " and last name is ".$_POST['lname'];
?>
This tutorial will
be about how you can stop submitting form when “Enter key”
pressed.
As you can see I
created one file with HTML form which submits form when “ENTER”
key is pressed.
Second file contains
php code to receive data.
Create a keydown
attribute and type this.
Make sure to type
event in rounded brackets.
Paste it at any
textbox you want.
Then create that
function in script tag. Dont forget to type evt argument.
Lets create a
boolean variable.
Lets alert the
keycode. C is capital.
If I hit “A” on
keyboard, it alerts me its code 65.
And 66 is for B.
The “ENTER” key
has keycode 13.
So we just have to
change boolean variable “FALSE”, then return it.
Let’s test it.
If I type any
keywords, it allows me to but it will not allow me to submit form
when
ENTER is pressed.
ENTER is pressed.
Thank you for
watching, dont forget to like share and subscribe.