Prototype - Form.Element present() Method


Advertisements


This method returns true if a text input has contents, false otherwise.

Syntax

formElement.present();

Return Value

It returns true if a text input has contents, false otherwise.

Example

Try to submit the following form first without filling out the information, then again after typing some text in the fields −

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var valid, msg = $('msg')

            // are both fields present?
            valid = $('username').present() && $('email').present()

            if (valid) {
               // in real world we would return true here to allow the 
               // form to be submitted return true
               msg.update('Passed validation!').style.color = 'green'
            } else {
               msg.update('Fill out all the fields.').style.color = 'red'
            }
            return false
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />

      <form id = "example" action = "#">
         <fieldset>
            <legend>User Details</legend>
            <p style = "color:green;" id = "msg">
               Fill out all the fields:
            </p>
            <div>
               <label for = "username">Username</label>
               <input id = "username" name = "username" type = "text">
            </div>
            <div>
               <label for = "email">Email Address</label>
               <input id = "email" name = "email" type = "text">
            </div>
            <div>
               <input value = "result" type = "button" onclick = "showResult()";>
            </div>
         </fieldset>
      </form>
      
   </body>
</html>

Output


prototype_form_management.htm

Advertisements