文本字段的表单验证无效?

时间:2022-04-14 07:44:41

In javascript, I´m supposed to create a function that checks if a textfield within a form is empty. If it is and the user clicks submit, the user will not be allowed to proceed. I found what I considered a suitable solution to this on w3schools (http://www.w3schools.com/js/js_validation.asp). I´ve checked more times than I can remember and everything seems to be in order, but it´s not working!! Instead, when the submit button is clicked, the website calls a different function I have in javascript which it is not supposed to do...

在javascript中,我应该创建一个函数来检查表单中的文本字段是否为空。如果是,并且用户单击“提交”,则不允许用户继续。我在w3schools上找到了我认为合适的解决方案(http://www.w3schools.com/js/js_validation.asp)。我检查的次数比我记忆的多,而且一切似乎都井然有序,但它不起作用!!相反,当点击提交按钮时,网站调用我在javascript中的不同功能,它不应该...

HTML code

Other code
<p>
   <form method="post" name="form" action="" onsubmit="return validateName()">
   <label for="fullName">Namn: </label><input id="fullName" class="text" name="namn" type="text"> </input>
</p>
<p>
   <label for="epost">Epost: </label><input id="epost" class="text" name="epost" type="email"> </input>
</p>
<p>
   <input id="submit" type="submit" value="Skicka"> </input>
</p>
</form>
Other code

Javascript code

function validateName() {
   var a = document.forms["form"]["namn"].value;
   if (a == null || a == "") {
      alert("Name required");
      return false;
   } else {
      return true;
   }
}

Clicking the submit button should call this function above (validateName), but instead it calls this function:

单击提交按钮应该在上面调用此函数(validateName),而是调用此函数:

function alert() { 
   return confirm("Do you really wish to leave this website?");
}

I´ve looked through my code multiple times and can´t find anything that seems to be out of place. Can any of you find anything wrong? And maybe suggest a solution that solves my problem so my function will work properly?

我多次查看我的代码,但找不到任何看似不合适的地方。你们中间有人发现任何错误吗?也许建议一个解决我的问题的解决方案,以便我的功能正常工作?

I would be very grateful if someone could help me resolve this matter!

如果有人能帮我解决这个问题,我将非常感激!

3 个解决方案

#1


0  

alert is a predefined function that you are using correctly once and incorrectly the second time. Simply change the name of YOUR alert function to something else, or just use confirm as it was intended and leave out the function alert part

alert是您正确使用的预定义函数,第二次正确使用。只需将您的警报功能的名称更改为其他名称,或者只是按预期使用确认,并省略功能警报部分

Correct:

alert("Name required");

Incorrect:

function alert() { 
   return confirm("Do you really wish to leave this website?");
}

one solution is to do this:

一个解决方案是这样做:

function confirm_leaving(){
    return confirm("Do you really wish to leave this website?");
}

#2


0  

That's because you are calling the alert function within your validateName function.

那是因为你在validateName函数中调用了alert函数。

function validateName() {
   var a = document.forms["form"]["namn"].value;
   if (a == null || a == "") {
      alert("Name required");  //<- remove this
      return false;
   } else {
      return true;
   }
} 

#3


0  

Well i think you may put this code instead of this : function validateName() { To correct this just put a this command : function validateName(this) {

好吧,我想你可以把这段代码改为:function validateName(){要纠正这个,只需输入一个命令:function validateName(this){

#1


0  

alert is a predefined function that you are using correctly once and incorrectly the second time. Simply change the name of YOUR alert function to something else, or just use confirm as it was intended and leave out the function alert part

alert是您正确使用的预定义函数,第二次正确使用。只需将您的警报功能的名称更改为其他名称,或者只是按预期使用确认,并省略功能警报部分

Correct:

alert("Name required");

Incorrect:

function alert() { 
   return confirm("Do you really wish to leave this website?");
}

one solution is to do this:

一个解决方案是这样做:

function confirm_leaving(){
    return confirm("Do you really wish to leave this website?");
}

#2


0  

That's because you are calling the alert function within your validateName function.

那是因为你在validateName函数中调用了alert函数。

function validateName() {
   var a = document.forms["form"]["namn"].value;
   if (a == null || a == "") {
      alert("Name required");  //<- remove this
      return false;
   } else {
      return true;
   }
} 

#3


0  

Well i think you may put this code instead of this : function validateName() { To correct this just put a this command : function validateName(this) {

好吧,我想你可以把这段代码改为:function validateName(){要纠正这个,只需输入一个命令:function validateName(this){