简体   繁体   English

Javascript多字段/表单验证

[英]Javascript Multiple Field/Form Validation

Yes, I know there are many questions on Stacked involving form validation but while some have been very close to what I'm trying to accomplish, I think this is unique. 是的,我知道有很多关于Stacked涉及表单验证的问题,但有些问题与我正在努力完成的事情非常接近,我认为这是独一无二的。

I have this JS fiddle with this script that I want to use that will return all the fields by name that have not been filled out. 我有这个 JS捣鼓这个剧本,我想使用将由尚未填写名称返回所有字段。 I feel this is a much better approach as I was doing this below code to try to accomplish the same result with multiple field validation: 我觉得这是一个更好的方法,因为我在下面的代码中这样做,试图通过多个字段验证来完成相同的结果:

function validate ( )
{
valid = true;

if ( document.contactinfo.Name.value == "" )
{
    alert ( "You need to fill the name field!" );
    valid = false;
}

  if ( document.contactinfo.email.value == "" )
{
    alert ( "You need to fill in your email!" );
    valid = false; //change variable valid to false
}
return valid;
}

While the above works and puts out multiple alert boxes, I manually have to alert them several times on what fields need to be filled out. 虽然上述工作并发出多个警报框,但我手动必须多次提醒他们需要填写哪些字段。 I'd much rather send out an alert that tells them what fields they are missing in one fell swoop and return the focus to those fields. 我宁愿发出一个警报,告诉他们他们一下子丢失了哪些字段,然后将焦点返回到那些字段。 The JS fiddle script does that, however, I keep getting the error that: ValidateRequiredFields is not defined and I don't see where the issue lies. JS小提琴脚本可以做到这一点,但是,我不断收到错误: ValidateRequiredFields is not defined ,我也看不到问题所在。 I've named everything correctly and the form data should be getting passed up. 我已正确命名所有内容,表单数据应该被传递出去。

Any thoughts? 有什么想法吗? As always, ask for clarification if needed. 与往常一样, 如果需要请求澄清。 Thanks! 谢谢!

Note: I would not like to use JQuery as I know they have very easy plugins that allow you to set required classes! 注意:我不想使用JQuery,因为我知道他们有非常简单的插件,允许您设置所需的类!

I dont know about the fiddle error, but about your script there are severall improvements: 我不知道小提琴错误,但关于你的脚本有几个改进:

you can improve that by collecting all messages into a string and do a single alert 您可以通过将所有消息收集到一个字符串中并进行单个警报来改进它

function validate ( ) {
  var valid = true;
  var msg="Incomplete form:\n";
  if ( document.contactinfo.Name.value == "" ) {
    msg+="You need to fill the name field!\n";
    valid = false;
  }
  if ( document.contactinfo.contact_email.value == "" ) {
    msg+="You need to fill in your email!\n";
    valid = false;
  }
  if (!valid) alert(msg);
  return valid;
}

improvement, return focus to first field in error and changing border color of fields with problems: 改进,将焦点返回错误的第一个字段并更改有问题的字段的边框颜色:

function validate ( ) {
  var valid = true;
  var msg="Incomplete form:\n";
  if ( document.contactinfo.Name.value == "" ) {
    if (valid)//only receive focus if its the first error
      document.contactinfo.Name.focus();
    //change border to red on error (i would use a class change here...
    document.contactinfo.Name.style.border="solid 1px red";
    msg+="You need to fill the name field!\n";
    valid = false;
  }
  if ( document.contactinfo.contact_email.value == "" ) {
    if (valid)
      document.contactinfo.contact_email.focus();
    document.contactinfo.contact_email.style.border="solid 1px red";
    msg+="You need to fill in your email!\n";
    valid = false;
  }
  if (!valid) alert(msg);
  return valid;
}

now, the above code signals error, return focus to first error and puts red borders on field with error... still we need some improvement. 现在,上面的代码发出错误信号,将焦点返回到第一个错误,并将红色边框置于字段上并出现错误......我们还需要一些改进。 first we need to remove red border once the field is valid.. this could be done with an else on each field check above... however i would be assuming that there is only one error condition for each field, and that might not be the case. 首先我们需要在字段有效时删除红色边框..这可以通过上面每个字段检查上的else来完成...但是我会假设每个字段只有一个错误条件,这可能不是案子。 ex: email field can check for not empty and for valid email 例如:电子邮件字段可以检查非空和有效电子邮件

one way to do that clean is to remove all red border at the beggining and then start validation. 清理的一种方法是删除开始处的所有红色边框,然后开始验证。

the style.border="..." is only a simplistic way of doing it, i would prefer a class change and a class remove if not on error. style.border =“...”只是一种简单的方式,我更喜欢类更改和类删除,如果没有错误。

the sugar on top would be: 顶上的糖将是:

-we need to remove the red border once the field becomes valid - 一旦字段生效,我们需要删除红色边框

-Make an array of all fields including names, conditions and messages - 创建所有字段的数组,包括名称,条件和消息

Automate the process by cycling the array. 通过循环阵列自动执行该过程。 That way we could do a cycle for cleaning borders and another to check conditions, ending with a compact and reusable code. 这样我们可以做一个循环来清理边框,另一个循环来检查条件,以紧凑和可重用的代码结束。

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

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