views:

72

answers:

4

Hi.

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?

+3  A: 

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:

Daniel Vandersluis
It says the following in IE: 'value' is null or not a object on the 5º line in your code. I edited the question to show how I'm calling the function.
StudioWorks
Is your form actually named `form`? Does the field name you are passing into `validate` exist?
Daniel Vandersluis
My form name is actually called 'questionario', I changed that. Also, as you can see in my question ( that I edited ) I'm calling the function passing the 'aa' field from inside the 'aa' field.
StudioWorks
@StudioWorks take a look at my comment on your question.
Daniel Vandersluis
A: 

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

Nico
Unless jQuery is already being used for something, this is really overkill.
Daniel Vandersluis
+2  A: 
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;
    }
Benj
+1  A: 

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.

Colin Fine