Forum Moderators: open

Message Too Old, No Replies

Help with functions and validation

         

cspitzig

4:19 pm on Oct 8, 2003 (gmt 0)

10+ Year Member



I have just created my 1st form validation.

I have two conditions to be tested and have written them in individual functions, but how do I get them to both run together?

Do I put them together in one function that calls both or do I set up if/else statements?

here is my code :

<!--

function validateEmpty(objcontrol)
{
var result = true;
//alert("hello world");
if (objcontrol.value == "" ¦¦ objcontrol == null){
alert("please enter something in");
objcontrol.focus();
objcontrol.select();
result = false;
}
return result;
}

function validateNumber(objcontrol)
{
var ValidChars = "0123456789.";
var result = true;
var Char;
var mystring = objcontrol.value

// alert(mystring.length);
for (i = 0; i < mystring.length && result == true; i++)
{
Char = mystring.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{

alert("Please enter numbers only");
objcontrol.focus();
objcontrol.select();
result = false;
}
}
return result;
}

-->
</script>

<form name="searchResult2" action="../donewPermitSearch.asp" method=POST onSubmit="javascript:return validateEmpty(this.PermitNO)" >

__________________

Colleen

RonPK

4:50 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Colleen, welcome to WebmasterWorld!

In pseudo-code, you could do this:

if condition 1 fails
then alert something and return false;
if condition 2 fails
then alert something and return false;

return false will exit the function, so there is no need for complicated if-else constructions.

[edited by: RonPK at 4:57 pm (utc) on Oct. 8, 2003]

DrDoc

4:54 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or, possibly even better:

function validateEmpty(objcontrol)
{
var result = true;
//alert("hello world");
if (objcontrol.value == "" ¦¦ objcontrol == null){
alert("please enter something in");
objcontrol.focus();
objcontrol.select();
return false;
}
validateNumber(objcontrol);
}

This will return false if the first check fails, otherwise it will call the second function (which will return either true or false).

RonPK

5:01 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry about the mess. I noticed that my first version wasn't a real answer to the question, so I edited it, but in the meantime DrDoc stepped in and gave his reply.