Can you please tell me how to make a field in a web form a required field?

  • one
    <input type = "text" name = "fld1" required /> - Alex Kapustin
  • required works only with the support of html5 it is better to organize a mandatory field check in addition - ikoolik
  • Via javascript? - archisova
  • The initial check can be done on JavaScript but if the data is important, it is necessary to recheck on the server ( php ) - ikoolik
  • On the server, ALWAYS need to recheck. - Alex Kapustin 2:01 pm

1 answer 1

As stated in the comments in HTML5, there really is an attribute required, but at the moment it does not work correctly in most browsers.

I advise you to implement a check using javascript (for security reasons, be sure to check on the server side). There are a lot of plugins on the Internet that can do this ( compilation for jQuery ).

In the simplest case, validation looks like this:

Javascript:

 function validateForm() { var x=document.forms["myForm"]["fname"].value; if (x==null || x=="") { alert("Необходимо заполнить поле Имя!"); return false; } } 

HTML:

 <form name="myForm" action="action.php" onsubmit="return validateForm()" method="post"> Имя: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> 
  • Oh thanks !!! - archisova
  • And tell me, if I have one more condition, the fields should always be 10, i.e. if the field is 10 then the data is sent, if everything else is well there is "empty", "null", "any character", then output the message that "Specify the correct value!" - archisova
  • one
    All decided! function validateForm () {var x = document.forms ["form"] ["fname"]. value; if (x == 10) {return true; } else {alert ("You must specify the correct result of the equation!"); return false; }} - archisova