0

im trying to put to javascript functions that will validate a form, but cant make it work. This is what i have :

<script type="text/javascript" language="JavaScript">
    function checkForm(f)
    {
        if (f.elements['val1'].value == "")
        {
            alert("Por favor insere o valor correcto do subsidio");
            return false;
        }
        else
        {
            f.submit();
            return false;
        }
    }

    function IsNumeric(sText)
    {
       var ValidChars = "0123456789.";
       var IsNumber=true;
       var Char;

       for (i = 0; i < sText.length && IsNumber == true; i++) 
       { 
           Char = sText.charAt(i); 
           if (ValidChars.indexOf(Char) == -1) 
           {
               alert("O valor que introduziu não é válido");
               IsNumber = false;
           }
       }
       return IsNumber;
    }
</script>

    <form method="post" onSubmit="return checkForm(this); return IsNumeric(this); return false;" action="<?php echo $_SERVER['PHP_SELF']; ?>">

The first function works, but the second one that will only allow numbers and decimal points dont work. (Not sure if the second function is right)

Can someone hel me out?

Sincerely

X Slash
  • 4,133
  • 4
  • 27
  • 35

3 Answers3

3

Simpler version

<script type="text/javascript" language="JavaScript">
function checkForm(f) {
  var val = f.elements['val1'].value; 
  if (val.length==0) { // blank
    alert("Por favor insere o valor");
    return false;
  }
  if (isNaN(val)) { // not numeric
    alert("Por favor insere o valor correcto do subsidio");
    return false;
  }
  // here the value is a number and filled in
  return true; // no need for else after a return
}
</script>

<form method="post" onSubmit="return checkForm(this);" action="<?php echo $_SERVER['PHP_SELF']; ?>">
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks for reply, it seems to work, but i need 2 diferent messages for the 2 diferent scenarios, also i need to allow numbers and decimal point. how can i acomplish this? thanks – Dani Queiroga Jan 27 '12 at 15:56
0

Try returning this instead:

<form onSubmit="return checkForm(this) && IsNumeric(this.elements['val1'].value)">

The return statement stops executing the proceeding code, and the IsNumber function is never called.

By the way: You can validate a number like this:

function IsNumeric(txt){ return parseFloat(txt) == txt; }
Jørgen
  • 8,820
  • 9
  • 47
  • 67
  • +1 for the && but -1 for the this being the form and how would parseFloat(string) ever be === string??? – mplungjan Jan 27 '12 at 11:11
  • Thanks for pointing out. It should of course be two equal signs, as the result needs to be string-compared. – Jørgen Jan 27 '12 at 11:53
0

You're returning the value of the first function and are therefore stopping the is number function from running.

You need to call is number in your checkForm function.

<script type="text/javascript" language="JavaScript">
function checkForm(f)
{
    if (f.elements['val1'].value == "")
    {
        alert("Por favor insere o valor correcto do subsidio");
        return false;
    }
    else
    {
        return IsNumeric(f.elements['val1'].value);
    }
}

    function IsNumeric(sText)

    {
       var ValidChars = "0123456789.";
       var IsNumber=true;
       var Char;


       for (i = 0; i < sText.length && IsNumber == true; i++) 
          { 
          Char = sText.charAt(i); 
          if (ValidChars.indexOf(Char) == -1) 
             {
            alert("O valor que introduziu não é válido");
             IsNumber = false;
             }
          }
       return IsNumber;

       }
    </script>

    <form method="post" onSubmit="return checkForm(this);" action="<?php echo $_SERVER['PHP_SELF']; ?>">
JConstantine
  • 3,980
  • 1
  • 33
  • 46