Click here to Skip to main content
16,020,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a working code that i use to validate empty fields, of which are required fields.
If fields are empty i hide the submit button. But on-load of the page, the button is hidden even if there is info inside input fields.

HTML CODE
HTML
<label for="firstname"><span class="starRequired">* </span>First name:</label><br><input  type="text" name="firstname" required  id="firstname" autofocus="autofocus" pattern ="{3,50}" >">

<label for="lastname"><span class="starRequired">* </span>Last name:</label><br><input type="text" name="lastname" required id="lastname"  pattern ="{3,50}" >">


 <label for="mobilenumber"><span class="starRequired">* </span>Mobile Number:</label><input type="text" name="mobilenumber" required  id="mobilenumber" >">


<label for="idnumber">ID Number:</label><br/>
<input type="text" name="idnumber"required  id="idnumber"  >">



PHP
if ($status != "2") { ?>

HTML
<input type="submit"  id="apply" name="apply"  value="Apply"  
/>
									
<div class="agreement">					
<input type="checkbox" id="terms" name="terms" >
<label for="terms">I accept the Reseller Agreement</label>



PHP
<?php } ?>



JavaScript
JavaScript
inputlastName = document.getElementById("lastname"); 
inputfirstName = document.getElementById("firstname");
inputmobileNumber = document.getElementById("mobilenumber");
inputidNumber = document.getElementById("idnumber");

inputSubmit = document.getElementById("apply"); 

function submitChange()
{
     if(inputfirstName.value == "" || inputlastName == "" || inputmobileNumber.value == "" || inputidNumber.value == "")
     {
          inputSubmit.style.display = "none";
     }
     else
     {
          inputSubmit.style.display = "block";
     }
	 
}



I want to prevent it form disappearing if I have values in fields on-load of the page.
How can I accomplish this?
Posted

1 solution

What you need to do is get submitChange() to be executed when the document/window has loaded.

This can be performed in a few ways on client side.
HTML:
HTML
<body onload="submitChange()">
</body>

JavaScript:
JavaScript
window.onload = function() {
  submitChange();
};

jQuery:
JavaScript
$(document).ready(function() {
    submitChange();
});

I believe the above 3 techniques should work.
 
Share this answer
 
Comments
iDoKin 2-Dec-15 5:39am    
This do not work for me.
jaket-cp 2-Dec-15 5:40am    
which one did you try?
jaket-cp 2-Dec-15 5:46am    
Just had another look a your javascript
inputlastName is missing value
iDoKin 2-Dec-15 5:48am    
I fixed that one.
jaket-cp 2-Dec-15 5:50am    
okay on that fix.
Which one of the 3 did you try to get the submitChange() to get executed after page load?

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