I am having an input field and a submit button:
<form action="" method="post">
<input type="text" name="sponsar_name" id="sponsar_name" />
<input type="submit" value="Submit" class="btn-submit" name="submit" onClick="validate()" >
</form>
function validate()
{
var flag = 0;
if(document.getElementById("sponsar_name").value == null || document.getElementById("sponsar_name").value == "")
{
document.getElementById("sponsar_name").style.borderColor = 'red';
flag = 1;
}
else
{
document.getElementById("sponsar_name").style.borderColor = '';
}
if(flag==1)
{
return false;
}
else
{
return true;
}
}
</script>
I have applied a function onClick on submit button .this I have applied for validation.
When I click on submit button it shows red background of textbox but refreshes the page immediately.
If the textbox is blank it should not refresh the page, but in my code it is doing so.
Can anyone help me?
onClick="return validate()"does the trick.