简体   繁体   English

使用Javascript进行基本表单验证

[英]Basic form validation with Javascript

I am trying to understand the function at (http://www.w3schools.com/js/js_form_validation.asp) and rework it (below) using getElementsByClassName to find a class attribute I added to an HTML input tag. 我试图了解(http://www.w3schools.com/js/js_form_validation.asp)上的函数,并使用getElementsByClassName对其进行以下工作(如下)以查找添加到HTML输入标签的类属性。 I'm very new to Javascript, and I do not understand why I have to add .value (W3 example) or .view (my example) to the end of the statement below in order for it to work. 我是Java语言的新手,我不明白为什么我必须在下面的语句末尾添加.value(W3示例)或.view(我的示例),才能使其正常工作。 As I understand it the first statement in the function says look in the document, and assign the variable X, to any input fields with the class attribute reqname. 据我了解,该函数中的第一条语句表示查找文档,并将变量X分配给具有类属性reqname的任何输入字段。

Thank you. 谢谢。

MY FUNCTION: 我的功能:

function validateForm()
{
var x=document.getElementsByClassName("reqname").view;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}

FUNCTION AT W3: W3的功能:

function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}

A form element such as an input field typically has a value , that is the string that has been entered into the input field. 诸如输入字段之类的表单元素通常具有一个value ,即已输入到输入字段中的字符串。

By specifying .value , they explicitly check whether the content of the element is null. 通过指定.value ,它们显式检查元素的内容是否为null。 Without it, they'd be checking if the element itself is null, ie if the element exists . 没有它,他们将检查元素本身是否为null,即元素是否存在

Your code has some further issues. 您的代码还有其他问题。 .view that you're using doesn't make sense. 您使用的.view没有意义。 Also, getElementsByClassName returns a list of elements, rather than a single element, so you won't be able to immediately access its .value . 另外, getElementsByClassName返回元素列表,而不是单个元素,因此您将无法立即访问其.value

If you know that there's just the one element, you could check getElementsByClassName("reqname")[0].value . 如果知道只有一个元素,则可以检查getElementsByClassName("reqname")[0].value If you want to validate the value of all "reqname" fields, you'd have to iterate over your collection and check each item individually: 如果要验证所有 “ reqname”字段的值,则必须遍历集合并分别检查每个项目:

var elements =document.getElementsByClassName("reqname");
for(var i = 0, l = elements.length; i < l; i++) {
   var x = elements[i].value;
   if(x == null || x == '') {
      alert("First name must be filled out");
      return false;
   }
}

the input field has more properties than just it's 'value' (which is the text being written in it). 输入字段不仅具有“值”(即写入其中的文本)的属性,还具有更多的属性。 So you'll have to state which property you want to access by appending the name (in your case 'value') with a '.'. 因此,您必须通过在名称(在您的情况下为“值”)后面加上“。”来声明要访问的属性。 If you really want to do form validation, also be sure to do it in your php file as well ! 如果您真的要进行表单验证,还请确保也在您的php文件中进行验证! Everything javascript does is on the client side, so people can - basically - ignore it and just send some random data to your .php file. javascript所做的所有工作都是在客户端进行的,因此人们基本上可以忽略它,而只是将一些随机数据发送到您的.php文件中。

it's more simple to write 写起来更简单

function ValidateForm() {
    var x = $("#inputOID").val();
    if (x == null || x == "")
        alert("......");
    return false;
}

so your function is ValidateForm. 所以你的函数是ValidateForm。 You declare a variable x, and give the value from an input by the input ID. 您声明一个变量x,并通过输入ID给出输入的值。 For example <input id="inputOID" .... /> . 例如<input id="inputOID" .... /> So x will contain the value of the input. 因此x将包含输入的值。 The you verify if is null or empty, and if is you will alert the user. 您验证是否为null或为空,如果是,则将警告用户。 Thats all! 就这样! Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM