views:

45

answers:

5

I have two text field as mandatory and rest of the of the fields are normal. Now i have to detect enter key and submit the form. I have done that but the issue is that when error occurs (for example : fields are null) then i have to stop submitting the form but still form is submitting.

<input autocomplete="off"  type="text" id="first_name"  size="10"
onkeypress="if(event.keyCode == 13){ 
document.getElementById('btn_search').click();
return false;}">

Here btn_search is the id of the search button and I have to execute it. but when field is null then form should not be submitted. Give me some suggestion ??

Even i make separate function in JavaScript file, I am getting same response. Actually I am facing this problem from last 48 hours.

A: 
document.getElementById('btn_search').click(function()
{
if($('#first_name').val() == "") {
// submit form here
}
});

That will check if the value of "first_name" is empty. It wouldn't be null, as it's a textbox. Or do you want to check for the existence of the textbox before you submit the form?

Liggi
+2  A: 

I would recommend you to perform the validation in the onsubmit action of the form instead of handling key presses in the inputs:

<form action="/foo" onsubmit="return validate();">
    <input autocomplete="off" type="text" id="first_name" size="10" />
    <input type="submit" value="Search" />
</form>

And in your validate function:

function validate() {
    var firstName = document.getElementById('first_name').value;
    var isFormValid = (firstName != '');

    return isFormValid;
}
Darin Dimitrov
Actually, when user press the ENTER key then, form should be submitted. and before that validation should be run to check it whether fields are empty or not.
Rahul Patil
@Rahul, that's exactly what my code illustrates.
Darin Dimitrov
A: 

You can do this too :)

<input autocomplete="off"  type="text" id="first_name"  size="10" onkeypress="if(this.value.replace(/^\s+|\s+$/g,'') != '' && event.keyCode == 13){ document.getElementById('btn_search').click(); return false;}">
Bandpay
A: 

Just add it to the onsubmit event in the form

<form action="http://google.com" onsubmit="return (document.getElementById('first_name').value != '')">
    <input autocomplete="off" type="text" id="first_name" size="10" />
</form>​

return (document.getElementById('first_name').value != '') just returns a boolean state for the input element, if its empty the bool would be false there for the onsubmit will fail causing the user to enter some details first.

RobertPitt
Friend, according to requirement as the user press the Enter key then i have to call the function for submitting the form. so Key press should be used to detect the key and then that funtion will transfer the call to the submit event.
Rahul Patil
Well if you press enter in any input element that initializes the submit event anyway, and when that's fired the on submit is fired, so that would not make a difference.
RobertPitt
A: 

At the end, i resolve the issue. Here, the explanation The thing which is missed by me, is to return the value. Here "btn_search" is id of button and through which a separate function is called. In which i have put up the validation. Now whole code is pasted over here. Please have a look.

    <input autocomplete="off"  type="text"  id="first_name" 
onkeypress="return enterKeyPress(event);" />

Javascript Function

//Enter-listener
function enterKeyPress(e)
{
    if (e.keyCode == 13) {
        document.getElementById('btn_search').click();
        return false;
        }
    }

Thanks a lot for the suggestions.

Rahul Patil
Rahul: Please consider using the onsubmit function of the form as Darin recommended. It's the standard way of doing form validation. It covers 'enter' being pressed in the field and the button being clicked without any of the code you show here being necessary.
Skip Head
@Skip Head : I am not saying that Darin Bro is wrong, but according to requirement i have to do like this. Otherwise Darin suggestion is really helpful for me to modify my further code. Thanks dear
Rahul Patil