How do I use javascript to prevent form submission because of empty fields?

后端 未结 4 1425
慢半拍i
慢半拍i 2021-01-24 01:44

How do I make a script in javascript to output an error and prevent form submission with empty fields in the form? Say the form name is \"form\" and the input name is \"name\".

4条回答
  •  爱一瞬间的悲伤
    2021-01-24 02:17

    HTML Code :-

    Javascript Code :-

      function runMyFunction()
        {
            if (document.getElementsByName("name")[0].value == "")
            {
                alert("Please enter value");
            }
            else
            {
                var form= document.getElementsByName("form")[0];
                form.submit();        
            }
        }
    

提交回复
热议问题