How can I ensure that my function is being called and running

Kode.Error404

I am creating a form validation here. I want to ask how does form validation works. The script i have here does not seem to return anything as the error given on the JS console is that the ValidateForm() function is undefined.

HTML:

    <form name="myForm" action="ControllerServlet" onsubmit="return validateForm()" method="post">
  <tr>
    <td class="newusertd">Name:<span class="price">*</span></td>
    <td class="newusertd">
      <input style="color: black;" type="text" name="name" class="Btn">
    </td>
  </tr>
  <tr>
    <td class="newusertd">Contact Number:<span class="price">*</span></td>
    <td class="newusertd">
      <input style="color: black;" type="text" name="phone" placeholder="98989898" class="Btn">
    </td>
  </tr>
  <tr>
    <td class="newusertd">Email:<span class="price">*<br></span></td>
    <td class="newusertd">
      <input style="color: black;" type="text" name="email" placeholder="[email protected]" class="Btn">
    </td>
  </tr>
  <tr>
    <td class="newusertd">Address:<span class="price">*</span></td>
    <td class="newusertd">
      <input style="color: black;" type="text" name="address" class="Btn">
    </td>
  </tr>
  <tr>
    <td class="newusertd">Password:<span class="price">*</span>
      <br>(8-16 Characters with
      <br>one UPPERCASE & numbers)</td>
    <td class="newusertd">
      <input style="color: black;" type="password" name="password" class="Btn">
  </tr>
  <tr>
    <td class="newusertd"></td>
    <td class="newusertd">
      <input style="color: black;" type="submit" value="Create Account" class="Btn" />
    </td>
  </tr>
</form>

JS:

 function validateForm() {
  var name = document.forms["myForm"]["name"].value;
  var phone = document.forms["myForm"]["phone"].value;
  var email = document.forms["myForm"]["email"].value;
  var add = document.forms["myForm"]["address"].value;
  var passwd = document.forms["myForm"]["password"].value;
  var matchesCount = email.split("@").length - 1;
  var matchesCount2 = email.split(".").length - 1;
  var error = "";
  if (!name || !phone || !email || !add || !passwd) {
    error += "All must be filled out \n"

    if (phone.search(/^[0-9]*$/) == -1 || phone.length != 8) {
      error += "- Phone number can only contain digits \n";
    }

    if (passwd.search(/^[0-9a-zA-Z]*$/) == -1) {
      error += "- Password needs to be alphanumeric \n";
    }

    if (passwd.length < 8 || passwd.length > 16) {
      error += "- Password contain only 8-16 digits \n";
    }

    if (matchesCount > 1 || matchesCount2 < 1) {
      error += "- Please enter a valid email \n";
    }
    alert(error);
    return false;
  }
}

https://jsfiddle.net/KodeError404/wpwv614c/

Please inform me if you cannot see the JSFiddle.

In the script I am checking if all fields are filled and if its not filled, it should return false. I would appreciate if this is kept strictly to JS and not JQuery.

EDIT: This issue is that the password does not seem to check if i key in anything or if its alphanumeric with Uppercase. I want to know why does all fields get checked but not my password field

Kode.Error404

I realised i could break up the regex for the password to check the conditions individually.

function validateForm() {
  var name = document.forms["myForm"]["name"].value;
  var phone = document.forms["myForm"]["phone"].value;
  var email = document.forms["myForm"]["email"].value;
  var add = document.forms["myForm"]["address"].value;
  var passwd = document.forms["myForm"]["password"].value;
  var matchesCount = email.split("@").length - 1;
  var matchesCount2 = email.split(".").length - 1;
  var error = "";
  if (name == '' || phone == '' || email == '' || add == '' || passwd == '') {
    error += "All must be filled out \n"
  } else {
    if (phone.search(/^[0-9]*$/) == -1 || phone.length != 8) {
      error += "- Phone number can only contain digits \n";
    }
    if (passwd.search(/[A-Z]/) == -1) {
      error += "- Password needs to be have one UPPERCASE\n";
    }

    if (passwd.search(/[a-z]/) == -1) {
      error += "- Password needs to be have letters\n";
    }

    if (passwd.search(/[0-9]/) == -1) {
      error += "- Password needs to be have numbers\n";
    }

    if (passwd.length < 8 || passwd.length > 16) {
      error += "- Password contain only 8-16 digits \n";
    }

    if (matchesCount > 1 || matchesCount2 < 1) {
      error += "- Please enter a valid email \n";
    }
  }
  alert(error);
  return false;
}
<form name="myForm" action="ControllerServlet" onsubmit="return validateForm()" method="post">
  <table>

    <tr>
      <td class="newusertd">Name:<span class="price">*</span>
      </td>
      <td class="newusertd">
        <input style="color: black;" type="text" name="name" class="Btn">
      </td>
    </tr>
    <tr>
      <td class="newusertd">Contact Number:<span class="price">*</span>
      </td>
      <td class="newusertd">
        <input style="color: black;" type="text" name="phone" placeholder="98989898" class="Btn">
      </td>
    </tr>
    <tr>
      <td class="newusertd">Email:<span class="price">*<br></span>
      </td>
      <td class="newusertd">
        <input style="color: black;" type="text" name="email" placeholder="[email protected]" class="Btn">
      </td>
    </tr>
    <tr>
      <td class="newusertd">Address:<span class="price">*</span>
      </td>
      <td class="newusertd">
        <input style="color: black;" type="text" name="address" class="Btn">
      </td>
    </tr>
    <tr>
      <td class="newusertd">Password:<span class="price">*</span>
        <br>(8-16 Characters with
        <br>one UPPERCASE & numbers)</td>
      <td class="newusertd">
        <input style="color: black;" type="password" name="password" class="Btn">
    </tr>
    <tr>
      <td class="newusertd"></td>
      <td class="newusertd">
        <input style="color: black;" type="submit" value="Create Account" class="Btn" />
      </td>
    </tr>

  </table>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

How can I ensure that correct function is called in case there are multiple candidates

From Dev

In angular how can I test a scope function is being called in my directive?

From Dev

How can I get a function name when it is being called?

From Dev

How can I ensure my application retains focus while SendKeys is called? (Is there a better way than using Sleep?)

From Dev

How can I ensure if there is no crypto currency mining script running on my ubuntu?

From Dev

How can I ensure if there is no crypto currency mining script running on my ubuntu?

From Dev

How can I check if my function is called with input from a pipe?

From Dev

How can I include a function (and its parameter) within an event handler, without the function being called on page load?

From Dev

How can I ensure that the callback for setInterval is called exactly at the interval specified?

From Dev

How can I ensure I am running the current stock kernel?

From Dev

How can I ensure I am running the current stock kernel?

From Dev

How can I make my function to run the moment the page is running?

From Dev

Why is my function not being called?

From Dev

How can I prevent the function from being called if the property is already set?

From Dev

How can I ensure my app can not be installed on Marshmallow

From Dev

How can I ensure my app can not be installed on Marshmallow

From Dev

How can I stop a long-running function when it is called multiple times?

From Dev

How can I ensure my cronjob will run at specified time?

From Dev

How can I ensure my cronjob will run at specified time?

From Dev

F# function running without being called

From Dev

Javasctipt function running without being called?

From Dev

jquery function running without being called

From Dev

How can I diagnose why viewDidLoad is being called repeatedly?

From Dev

How can I ensure a reactjs state is updated, and then call a function?

From Dev

How can I select the currently clicked div? and why is my function not being executed?

From Dev

How can I return a called back function?

From Dev

How can I return a called back function?

From Dev

How can I tell if whether my PowerShell module function was called by C# application or from the command line?

Related Related

  1. 1

    How can I ensure that correct function is called in case there are multiple candidates

  2. 2

    How can I ensure that correct function is called in case there are multiple candidates

  3. 3

    In angular how can I test a scope function is being called in my directive?

  4. 4

    How can I get a function name when it is being called?

  5. 5

    How can I ensure my application retains focus while SendKeys is called? (Is there a better way than using Sleep?)

  6. 6

    How can I ensure if there is no crypto currency mining script running on my ubuntu?

  7. 7

    How can I ensure if there is no crypto currency mining script running on my ubuntu?

  8. 8

    How can I check if my function is called with input from a pipe?

  9. 9

    How can I include a function (and its parameter) within an event handler, without the function being called on page load?

  10. 10

    How can I ensure that the callback for setInterval is called exactly at the interval specified?

  11. 11

    How can I ensure I am running the current stock kernel?

  12. 12

    How can I ensure I am running the current stock kernel?

  13. 13

    How can I make my function to run the moment the page is running?

  14. 14

    Why is my function not being called?

  15. 15

    How can I prevent the function from being called if the property is already set?

  16. 16

    How can I ensure my app can not be installed on Marshmallow

  17. 17

    How can I ensure my app can not be installed on Marshmallow

  18. 18

    How can I stop a long-running function when it is called multiple times?

  19. 19

    How can I ensure my cronjob will run at specified time?

  20. 20

    How can I ensure my cronjob will run at specified time?

  21. 21

    F# function running without being called

  22. 22

    Javasctipt function running without being called?

  23. 23

    jquery function running without being called

  24. 24

    How can I diagnose why viewDidLoad is being called repeatedly?

  25. 25

    How can I ensure a reactjs state is updated, and then call a function?

  26. 26

    How can I select the currently clicked div? and why is my function not being executed?

  27. 27

    How can I return a called back function?

  28. 28

    How can I return a called back function?

  29. 29

    How can I tell if whether my PowerShell module function was called by C# application or from the command line?

HotTag

Archive