最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Uncaught TypeError: Cannot read property 'reset' of null in Javascript - Stack Overflow

programmeradmin4浏览0评论

I am trying to validate email in a form input by a button click. Here is the code:

function ValidateEmail(inputText) {
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if (inputText.value.match(mailformat)) {
    document.form1.coemail.focus();
    return true;
  } else {
    alert('You have entered an invalid email address!Enter again');
    document.getElementById('form1').reset();
    return false;
  }
}

<button class="btn btn-primary nextBtn btn-lg pull-right" type="button"onClick="ValidateEmail(document.form1.coemail)">Next</button>

On clicking the Next button I get an alert message correctly but after that it redirects to next page instead of resetting the current one. On inspecting element in chrome's console,I found this error: Uncaught TypeError: Cannot read property 'reset' of null

Where am I wrong here ?

I am trying to validate email in a form input by a button click. Here is the code:

function ValidateEmail(inputText) {
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if (inputText.value.match(mailformat)) {
    document.form1.coemail.focus();
    return true;
  } else {
    alert('You have entered an invalid email address!Enter again');
    document.getElementById('form1').reset();
    return false;
  }
}

<button class="btn btn-primary nextBtn btn-lg pull-right" type="button"onClick="ValidateEmail(document.form1.coemail)">Next</button>

On clicking the Next button I get an alert message correctly but after that it redirects to next page instead of resetting the current one. On inspecting element in chrome's console,I found this error: Uncaught TypeError: Cannot read property 'reset' of null

Where am I wrong here ?

Share Improve this question edited Jan 30, 2019 at 13:02 EugenSunic 13.7k15 gold badges66 silver badges93 bronze badges asked Jun 26, 2015 at 2:31 sayonaraa1sayonaraa1 3841 gold badge4 silver badges18 bronze badges 6
  • 2 Apparently the html element with id "form1" couldn't be found in the document. – m69 ''snarky and unweling'' Commented Jun 26, 2015 at 2:36
  • Post your form code. form has id form1? – Ajay Narain Mathur Commented Jun 26, 2015 at 2:36
  • @A.J Yes . <form role="form" name="form1" action="reg_employer.php" method="post"> – sayonaraa1 Commented Jun 26, 2015 at 2:37
  • @shri_wahal: try the way i answered, or add id t form as <form id="form1" role="form" name="form1" action="reg_employer.php" method="post"> – Ajay Narain Mathur Commented Jun 26, 2015 at 2:39
  • @m69 Thank you so much for pointing out the error :) It now works perfectly !! Cheers . Apparently I didnt have a form id . – sayonaraa1 Commented Jun 26, 2015 at 2:41
 |  Show 1 more ment

2 Answers 2

Reset to default 6

Add id to your form:

<form id="form1" role="form" name="form1" action="reg_employer.php" method="post">

or

Try:

function ValidateEmail(inputText)
{
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if(inputText.value.match(mailformat))
{
  document.form1.coemail.focus();
  return true;

}
else
{
  alert("You have entered an invalid email address!Enter again");
  document.form1.reset();   //<== change this line, use form name
  return false;
}
}

Apparently I didn't have a form id in

<form id="form1" role="form" name="form1" action="reg_employer.php" method="post">

Addition of a form id resolved the issue.

发布评论

评论列表(0)

  1. 暂无评论