Click to See Complete Forum and Search --> : validation in javascript


cheers
02-22-2003, 10:21 AM
hi all, i had a question on validation. if i have a text field for user to key in data. the validation function in the javascript will help mi to check whether user had key any data.

If the user didnt key any data I wanted to put a "*" beside the text field. How could i do that?? Thanks alot.

rellik
02-22-2003, 11:53 AM
a function when the form is submitted that checks if the field is null, and if it is, then display an error message and/or display a layer with the *

cheers
02-22-2003, 12:12 PM
Sorry i do not know how to display a layer of "*". For my current code it goes like this...

cheers
02-22-2003, 12:16 PM
Sorry i do not know how to display a layer of "*". For my current code it goes like this...

function linkto()
{
if(loginPage.loginID.value == "")
{
alert("Please enter ID");
}
else if(loginPage.loginPwd.value == "")
{
alert("Please enter a password");
}
else if(loginPage.dsn.value == "")
{
alert("Please enter a domain");
}
else
{
document.loginPage.submit();
}
}
</script>


<INPUT TYPE="button" VALUE="Login" onClick="linkto()">&nbsp;&nbsp;
<INPUT TYPE="Reset" VALUE="Clear">

My form name is called loginPage... how should i implement the layer of "*"?? can help?? thankss..

Charles
02-22-2003, 12:21 PM
Keeping in mind that 10% of users do not use JavaScript, the way to get the asterisies is to do your validation server side and then to return a document with the little critters in place.

cheers
02-22-2003, 12:31 PM
sorri.. my project spec is to do the validation in the client side before passing it to the server side. do u have any solution for that?

Charles
02-22-2003, 12:43 PM
I'd give up on the asterisies with the client side version and just have them with the server side version. And you will need a valid FORM element if you are going to have a server side back up. Your two buttons should look like:

<INPUT TYPE="submit" VALUE="Login">
<INPUT TYPE="Reset" VALUE="Clear">

Your opening FORM tag should look like:

<FORM action="linkTo.pl" onsubmit="return linkTo(this)">

And your function shoule look like:

function linkTo(f) {
if(f.loginID.value == "") {alert("Please enter ID"); f.loginID.focus(); return false};
if(f.loginPwd.value == "") {alert("Please enter a password"); f.loginPwd.focus(); return false};
if(f.dsn.value == "") {alert("Please enter a domain"); f.dsn.focus(); return false};
return true;
}

Charles
02-22-2003, 12:52 PM
Another option would be to use the following function:

function linkTo(f) {
if(/^\*?$/.test(f.loginID.value)) {f.loginID.value='*'; invalid = true};
if(/^\*?$/.test(f.loginPwd.value)) {f.loginPwd.value='*'; invalid = true};
if(/^\*?$/.test(f.dsn.value)) {f.dsn.value='*'; invalid = true};
if (invalid) {alert ('Fields marked with an asterisk were left empty.'); return false} else {return true};
}

cheers
02-22-2003, 01:00 PM
thanks alot. i will try it. thanks :)