-4
<script>
function showUploading() {

    if (document.form['form']['name'].value != "" && 
        document.form['form']['city'].value != "" )) {

        document.getElementById('submit')   .style.visibility = "hidden";
        document.getElementById('uploading').style.visibility = "visible";
    }
}                       
</script>

With above script I'd like to verify if "name" and "city" form inputs are not empty. Somehow I can't get this working, condition is still returning false even if inputs are filled with text. Here are inputs:

<input required="required" id="name" name="name" type="text">
<input required="required" id="city" name="city" type="text">

I was also trying:

if (document.getElementById('name').value != "")

None of the above methods worked for me. Whats wrong here?

Tomas Turan
  • 1,195
  • 3
  • 17
  • 27

4 Answers4

0

you have an error in your code, there is one closing bracket too much

change

if (document.form['form']['name'].value != "" && 
    document.form['form']['city'].value != "" )) {

to

 if (document.form['form']['name'].value != "" && 
    document.form['form']['city'].value != "" ) {
john Smith
  • 17,409
  • 11
  • 76
  • 117
0
if(  document.getElementById("ValidateUsename").innerHTML =="")
{
alert("box is empty")
}
Coder
  • 240
  • 2
  • 4
  • 20
0

There are several things that might be wrong here

firstly you have a javascript issue on you 'if' statement (too many brackets)

you are using ['form'] but there is no form element shown. you have document.getElementById('submit') but there is no submit element shown and you have document.getElementById('uploading') but there is no uploading element.

Without knowing if they exist there is no way of knowing what the problem is. you also say that 'None of the above methods worked for me' but what IS happening? is anything happening, surley you have console errors?

atmd
  • 7,430
  • 2
  • 33
  • 64
0

try like this: you have syntax error in if condition.

function showUploading() {

    if (document.getElementById('name').value.trim() != "") {// trim to avoid initial spaces
            alert(document.getElementById('name').value);
        //document.getElementById('submit')  .style.visibility = "hidden";
        //document.getElementById('uploading').style.visibility = "visible";
      
      //rest of your code
    }
}    
<input required="required" id="name" name="name" type="text">
<input required="required" id="city" name="city" type="text">

<button onclick="showUploading()">click</button>
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44