-1

I am a student. I am trying to make a Jquery form validation in PHP file. But I do not know why it does not working. I made a other file but I did not have problems.

The main is to make the validation of the strName textfield, with a Jquery function that show the div with id userName, if the text-field is null.

I checked everything but, I did not get the mistake.

Here is my code:

$(document).ready(function() {
  function cancel() {
    location.href = "internalusers.php";
  }

  function validation() {
    valid = true;
    $('#userName').hide('fast');
    // Validation for Name of user.
    if (document.newuserform.strName.value == "") {
      $('#userName').show('fast');
      document.newuserform.strName.style = 'boder:1px solid red;';
      valid = false;
    }
    return valid;
  }
});
.userformfields {
  padding: 10px;
  margin: 3px;
  border: 1px solid #006A9D;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
}
.userformbutton {
  cursor: pointer;
  color: #FFF;
  background-color: #069;
  margin-bottom: 8px;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid #36C;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  box-shadow: 2px 2px 2px #999;
}
.error {
  display: none;
  font-size: 12px;
  padding: 5px;
  margin: 3px;
  background-color: #FF9797;
  color: #B00;
  border: 1px solid #990000;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  font-weight: 100;
}
.erraseuserbutton {
  cursor: pointer;
}
<!doctype html>
<html>

<head>
  <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
  <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>

<body>
  <div class="iconscontainer">
    <form action="<?php echo $editFormAction; ?>" method="post" name="newuserform" id="newuserform" onSubmit="return validation();">
      <table align="center">
        <tr valign="baseline">
          <td nowrap align="right">Nombre:</td>
          <td>
            <input class="userformfields" type="text" name="strName" value="" size="32">
            <div id="userName" class="error">Debe ingresar un nombre.</div>
          </td>
        </tr>
        <tr valign="baseline">
          <td nowrap align="right">&nbsp;</td>
          <td>
            <input class="userformbutton" type="submit" value="Crear nuevo usuario">
            <input name="cancelbutton" class="userformbutton" type="button" value="Cancelar" onClick="cancel();">
          </td>
        </tr>
      </table>
      <input type="hidden" name="MM_insert" value="newuserform">
    </form>
  </div>
</body>

</html>

Help please!

1

2 Answers 2

2

You wrapped the code in a document.ready method so the validation method is not global. So you can not call the function. DO NOT wrap all your code in document.ready

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I unwrap the code of the documente.ready. The function cancel works perfect, but the Jquery code does not. :-(
0

In your <Form> tag you might want to use onSubmit="validation();" instead of onSubmit="return validation();"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.