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

php - How to validate all fields with the same function in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I need to do the following, but I couldn't find any example of similar form validation on the web:

 <script type="text/javascript">
   function something(){
     if the value on the field who is calling this function == 2,4,6 or 8
     alert("This number is invalid")
     focus in this field. 
</script>

Field1 call something()
Field2 call something()
Field3 call something()
Field4 call something()
Field5 call something()
Field6 call something()

I've tried like this:

function validate()
        {
            valid = true;

            if ( document.form.field_name.value == "2" || document.form.field_name.value == "4" || document.form.field_name.value == "6" ||
           document.form.field_name.value == "8" ){
                alert ( "Invalid number." );
                valid = false;
                document.form.field_name.focus();

            }

            return valid;
        }

I'm calling the function like this:

a)"Some text" <input type="text" name="aa" size="1" maxlength="1" onkeypress="return validate(aa)"><br>

But this way I would have to create a different function for every field. So how could I implement this?

I need to do the following, but I couldn't find any example of similar form validation on the web:

 <script type="text/javascript">
   function something(){
     if the value on the field who is calling this function == 2,4,6 or 8
     alert("This number is invalid")
     focus in this field. 
</script>

Field1 call something()
Field2 call something()
Field3 call something()
Field4 call something()
Field5 call something()
Field6 call something()

I've tried like this:

function validate()
        {
            valid = true;

            if ( document.form.field_name.value == "2" || document.form.field_name.value == "4" || document.form.field_name.value == "6" ||
           document.form.field_name.value == "8" ){
                alert ( "Invalid number." );
                valid = false;
                document.form.field_name.focus();

            }

            return valid;
        }

I'm calling the function like this:

a)"Some text" <input type="text" name="aa" size="1" maxlength="1" onkeypress="return validate(aa)"><br>

But this way I would have to create a different function for every field. So how could I implement this?

Share Improve this question edited Sep 20, 2010 at 14:35 StudioWorks asked Sep 20, 2010 at 13:56 StudioWorksStudioWorks 4932 gold badges6 silver badges27 bronze badges 2
  • Some of the solutions below require that you attach the validation to each form element, and some are scripts that you can run onsubmit before the form posts. Both are good approaches. – Benj Commented Sep 20, 2010 at 14:05
  • The name of your field is a string, not an object or variable, so you need to quote it in your onkeypress attribute (onkeypress="return validate('aa')") or use this.name: onkeypress="return validate(this.name)" – Daniel Vandersluis Commented Sep 20, 2010 at 14:41
Add a ment  | 

5 Answers 5

Reset to default 3

You can use bracket notation instead of dot notation when collecting your form element, which allows you to use a variable:

function validate(field_name)
{
  var valid = true;
  var element = document.form[field_name];

  if (!element)
  {
    alert('A field named ' + field_name + ' cannot be found in your form!');
    return false;
  }

  var value = element.value;

  if (value == "2" ||  value == "4" || value == "6" || value == "8")
  {
    alert("Invalid number.");
    valid = false;
    element.focus();
  }

  return valid;
}

Then you'd just call validate with the name of the field you want to validate.

Resources:

  • Dot Notation and Square Bracket Notation in JavaScript
function validate()
    {
        var valid = true;

        var fields = ['list', 'of', 'unique', 'input', 'ids']; 

        for (inputid in fields) {
            field = document.getElementById(inputid);
            if ( field.value == "2" || field.value == "4" || field.value == "6" ||
       field.value == "8" ) {
                alert ( "Invalid number." );
                valid = false;
                field.focus();
                break;
            }
        }

        return valid;
    }

You need to parameterise your function, to pass the field (or its name in as an argument).

My example uses the prototype.js function $F, but you could do it equally well with jquery or (a bit more long-windedly) in native Javascript

function validate(field)
     {
            valid = true;
            value = $F(field);


            if ( value == "2" || value == "4" || value == "6" ||
           value == "8" ){
                alert ( "Invalid number." );
                valid = false;
                document.form.field_name.focus();

            }

            return valid;
        }

validate('field1');
validate('field2');

etc.

you could try using jQuery, and validate with something like this:

var valid = true;

$(".validateMe").each(function()
{
    var v = this.value;

    if(v == "2" || v=="4" || v=="6" || v=="8") {
        valid = false;

        // Exit for each
        return false;
    } 
});

if(!valid){
    alert("Invalid number.");
    $(this).focus();
}

return valid;

Each field would have to have the "validateMe" CssClass

Using Javascript
1. The if-else statements given on this website can be added to your validate function inside a loop that checks every field of the form.
http://eisabainyo/weblog/2009/04/30/10-examples-of-basic-input-validation-in-javascript/

2. Still unclear then refer this great guide
http://www.tizag./javascriptT/javascriptform.php

Using jQuery
You can always try jQuery validation plugins
http://speckyboy./2010/06/22/50-jquery-plugins-for-form-functionality-validation-security-and-customisation/

发布评论

评论列表(0)

  1. 暂无评论