Copyright ©

Prototype Form.Element present() Method

previous next


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



Return Value :


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

<title>Prototype examples</title>
   <script type="text/javascript" 
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


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

  <form id="example" action="#">
    <legend>User Details</legend>
    <p style="color:green;" id="msg">
	Fill out all the fields:
      <label for="username">Username</label>
      <input id="username" name="username" type="text">

      <label for="email">Email Address</label>
      <input id="email" name="email" type="text">
       <input value="result" type="button" onclick="showResult();>


To understand it in better way you can Try it yourself.

previous next

Copyright ©