Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the form field values before submitting that page

I have written following function which checks whether start_date field is not empty and displays proper message when submit button is clicked. But then it takes the control to the previous page. So user has to write again all other fields on that form. Is there any way to stay on that page even after prompting the error message, with all other fields value.

//JavaScript
function checkform() {
    if(document.frmMr.start_date.value == "") {
        alert("please enter start_date");
        return false;
    } else {
        document.frmMr.submit();
    }
}

// HTML
<html>
    <form name=frmMr action="page1.jsp">
        Enter Start date:
        <input type="text" size="15" name="start_date" id="start_date">
        <input type="submit" name="continue" value="submit" onClick="checkform();">
    </form>
</html>

Thanks in advance

like image 425
reva Avatar asked Jul 19 '10 08:07

reva


People also ask

How do you call a function before submitting a form?

Here we are calling a validate() function before submitting a form data to the webserver. If validate() function returns true, the form will be submitted, otherwise it will not submit the data. The HTML code snippet.


1 Answers

While you have a return value in checkform, it isn't being used anywhere - try using onclick="return checkform()" instead.

You may want to considering replacing this method with onsubmit="return checkform()" in the form tag instead, though both will work for clicking the button.

like image 72
user11977 Avatar answered Sep 21 '22 19:09

user11977