Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having a simple html form with the required validator set to one of its input field, but I do not get the validation message
Can you please help where am I going wrong??

What I have tried:

This is my code

<form id="newrentalform">
    <div class="form-group">
        <label>Customer</label>
        <div class="tt-container">
            <input id="customer" name="customer" type="text" class="form-control" required>
         </div>
    </div>

    <div class="form-group">
        <label>Movie</label>
        <div class="tt-container">
            <input id="movie" name="movie" type="text" value="" class="form-control">
        </div>
    </div>

    <div class="row">
        <div class="col-md-4">
            <ul id="movies" class="list-group"></ul>
        </div>
    </div>

   

    <button  class="btn btn-primary">Submit</button>
</form>




I have tried a demo with the required atribute it works in the demo but fails in case of the above code
Posted
Updated 30-Jun-20 21:31pm
v2

1 solution

I presume that customer input is the element you are talking about.

Javascript will return a message if no value has been added, maybe why it does not work in your sample as the java code was not copied over. With the required field, it will only show a small pop-up at the element that says required in Chrome, no messages at all.

HTML
<pre><button class="btn btn-primary" onclick="validateme();">Submit</button>


JavaScript
<pre><script type="text/javascript">
		function validateme() { 
			var customer = document.getElementById('customer');

			if (customer.length == 0 && customer.value == "") {
				alert('No valid customer added.');
				document.getElementById('customer').focus();
}
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900