I created a page where the user is asked to do a search query by inserting a name, an address or a phone number, which correspond to three navigation tabs ("Name", "Address" and "Number") and each of them is associated to an input text form and a "Search" button.
What I want to do now is displaying an error log whenever the user clicks on the "Search" button without having filled the corresponding field. For example, if the user is on the "Name" Tab and clicks on the "Search" button, an error message saying "Please insert a name" is displayed.
I included the error message in a <p>
tag in the html file and I want to use the JQuery .show()
and .hide()
methods to show such a message only when the two conditions below occur simultaneously:
1) the field is empty;
2) the button is clicked.
Otherwise, the message is hidden.
Here is my Javascript snippet, it doesn't work for my purpose:
function validateName(){
if($("#inlineFormInputName").val()==""){
$("#searchNome").on('click', $("#errorLog").show());
}
}
function validateAddress(){
if($("#inlineFormInputAddress").val()==""){
$("#searchAddress").on('click', $("#errorLog").show());
}
}
function validatePhone(){
if($("#inlineFormInputTelefono").val()==""){
$("#searchPhone").on('click', $("#errorLog").show());
}
}
function main(){
$("#searchNome").on('click', validateName);
$("#searchAddress").on('click', validateAddress);
$("#searchPhone").on('click', validatePhone);
}
$(main());
The #ID correspond respectively:
inlineFormInputName = id of the input form - first Tab;
searchName = button ID - firs Tab;
errorLog = id of the
<p>
containing the error messageinlineFormInputAddress = id of the input form corresponding to the 2ndTab;
searchAddress = button ID - 2nd Tab;
inlineFormInputTelefono = id of the input form corresponding to the 3rd Tab;
searchPhone = button ID - 3rd Tab.
I upload a screen of the webpage:
How may I show an error message only if the two conditions are satisfied, by using the JQuery .show()
and .hide()
methods?