0

Hope you all are fine . I have a problem my javascript function is not being called i dont know why I am very confused why the function is not calling I used this same flow in my another form too that was also working fine but here Its a mess Kinldy have a look on code and help me

Thanks

javascript:

function checkPriceValidation(){
    var flight=document.getElementById('flightno').value;
    var type=document.getElementById('type').value;
    var price=document.getElementById('price').value;

    if(flight==="volvo"){
        alert("Please select flight number !!");
        return false;
    }
    else if(type==="default"){
        alert("Please select type!!");
        return false;
    }
    else if(price===""){
        alert("Please select the price !!");
        return false;
    }
    return false;
}

html :

<form 
    class="" 
    method="post" 
    onsubmit="return javascript:checkPriceValidation();"
    action="<%=response.encodeURL(request.getContextPath()+"/FillList")%>"
>
    <div style="color:red;font-size:18px ">${error}</div>
    <% session.setAttribute("error", "");%>
    <table>
        <tr>
            <td>
                <div class="form-control" style="width: auto">
                    <select style="color: black" name="flightno" >
                        <option value="volvo">Select Flight</option>
                        <%
                            ArrayList<FlightClass> list;
                            list = (ArrayList<FlightClass>) request.getSession().getAttribute("list");
                            for (FlightClass post : list) {
                        %>
                            <option><%=post.getID()%></option>
                        <%  }  %>
                    </select>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="form-control" style="width: auto">
                    <select style="color: black" name="type">
                        <option value="default">Select Type</option>
                        <option>Economy</option>
                        <option>Business</option>
                        <option>First Class</option>
                    </select>
                </div>
            </td>
            <td>
                <input 
                    type="number"
                    min="0" 
                    step="1"
                    class="form-control"
                    name="price" 
                    id="price"
                    placeholder="Price"
                />
            </td>
        </tr>
    </table>
    <br/>
    <button input-type="submit" value="Submit" class="btn btn-lg btn-primary" >SET</button> 
</form>

1 Answer 1

1

In your onsubmit attribute, you should just use return checkPriceValidation(). Try out the code below and see the difference

function submitThis(){
  alert('Hi there');
  return false;
}
<form onsubmit="return javascript:submitThis()">
  <button>This form will be submitted</button>
</form>

<form onsubmit="return submitThis()">
  <button>This form will not be submitted</button>
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

Your code is slightly contradictory to your statement. I assume you meant for the button text to be the other way around. Otherwise that works great for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.