StudioWorks
StudioWorks

Reputation: 493

How to validate all fields with the same function in JavaScript?

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?

Upvotes: 0

Views: 4881

Answers (5)

Saurabh Rana
Saurabh Rana

Reputation: 3540

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.net/weblog/2009/04/30/10-examples-of-basic-input-validation-in-javascript/

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

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

Upvotes: 0

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94173

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:

Upvotes: 3

Benj
Benj

Reputation: 1875

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;
    }

Upvotes: 2

Colin Fine
Colin Fine

Reputation: 3364

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.

Upvotes: 1

bevacqua
bevacqua

Reputation: 48496

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

Upvotes: 0

Related Questions