Jump to content
xisto Community
Sign in to follow this  
vizskywalker

Form Validation Javascript

Recommended Posts

Okay, I know how to use cgi or php to make sure a form is filled out the way I want, and if not, post up a page marking what needs to be fixed. I also know how to make a javascript to alert what needs to be fixed and not procede until they are fixed. What I want to do is use javascript to mark everything that needs to be fixed without using alerts, probably by changing the color. Any ideas as to how to do this?

Share this post


Link to post
Share on other sites

This is quite simple, you could implement it many ways but similar to the following.Using table to lay out your form but provide 1 more column at the end (This mean 1 more cell for each row). Leave that column empty but formatted and named.Whenever your javascript code detects any field that needs to be fixed, assign the innerHTML property of the cell of the row contained the field.Syntax: <cell name>.innerHTML = "Name could not be empty";Hope this helpful,Any further questions, just post them up!!!

Share this post


Link to post
Share on other sites

Ok, this is what you did wrong (and me too, lol). Just copy and replace your code. It should work (cause I tested). Remember to format text in the error cell using html (like wat i did) or stylesheet (more professional).

 

 

<html>

<head>

<link rel="stylesheet" href="http://forums.xisto.com/no_longer_exists/; type="text/css">

<link rel="shortcut icon" type="image/x-icon" href="/images/Logo.ico">

<title>Mouse Isle Games: Sign Up</title>

 

<script language="javascript">

 

function verify() {

var proceed = true;

var email=signup.email.value;

if (!(email.indexOf(' ')==-1 && 0<email.indexOf('@') && email.indexOf('@')+1 < email.length)) {

maile.innerHTML = "<font color=#FF0000>Problem</font>";

proceed = false;

}

return(proceed);

}

 

</script>

 

</head>

<body>

<form name="signup" action="signup.php" method=post onSubmit="return(verify());">

 

<table>

<tr>

<td>Personal Info:</td>

</tr>

<td></td>

<tr>

<td>Name:</td>

</tr>

<tr>

<td>First:</td>

<td><input type=text name="fname" value=""></td>

<td id="firstn"></td>

</tr>

<tr>

<td>Last:</td>

<td><input type=text name="lname" value=""></td>

<td id="lastn"></td>

</tr>

<tr>

<td>E-mail address:</td>

<td><input type=text name="email" value=""></td>

<td id="maile"></td>

</tr>

<tr>

<td></td>

</tr>

<td>

</td>

<tr>

<td>User Info:</td>

</tr>

<tr>

<td>Username:</td>

<td><input type=text name="username" value=""></td>

<td id="uname"></td>

</tr>

<tr>

<td>Password:</td>

<td><input type=password name="password" value=""></td>

<td id="pword"></td>

</tr>

<tr>

<td><input type=submit value="Log In"></td>

</tr>

</table>

</form>

</body>

</html>

Share this post


Link to post
Share on other sites

Note.If you use a javascript check you need to check twice for good input because your PHP/asp/whatever language needs to check it too. So it might be better to use the parser check it and echo errors. Since javascript can be turned off so the checking is not trustworthy, using the parser is more secure too.

Share this post


Link to post
Share on other sites

Okay, I know how to use cgi or php to make sure a form is filled out the way I want, and if not, post up a page marking what needs to be fixed.  I also know how to make a javascript to alert what needs to be fixed and not procede until they are fixed.  What I want to do is use javascript to mark everything that needs to be fixed without using alerts, probably by changing the color.  Any ideas as to how to do this?

<{POST_SNAPBACK}>


Below is a verifier java script:

You should copy and paste below code to head

 

<script language="JavaScript1.2">

// JavaScript Document

// Pars Loyal Alert

function opensentwin(openurl)

{

window.open(openurl,null,'width=540,height=150,scrollbars=no,left=200,top=200');

}

 

function check_empty() //cheek name field

{

if (form.name.value=="")

{

alert(âPlease insert your name");//display alert message

form.name.focus();

return false;

}

 

if (form.mail.value=="")//cheek mail field

{

alert("Please insert your email");//display alert message

form.mail.focus();

return false;

}

//cheek that email be correct

if (form.mail.value.indexOf('@')<=0 || form.mail.value.indexOf('@')==form.mail.value.length-1 )

{

alert("youremail@site.com ");//display alert message

form.mail.focus();

return false;

}

 

} </script>

 

you should add following code as your submit button:

 

<input name="submit" type=submit onclick="return check_empty()" VALUE="Submit">

 

and you should set below properties to your fields

 

<input type=text name="name" size="10">

<input type=text name="mail" size="10">

you can change and modify code

you can test code on my site

http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

Here is a form validation I always use, it uses an alert box instead of HTML. I will do it using an example of 3 fields, name, email and password.

<HTML><HEAD><script language="javascript" type="text/javascript">function valform() {var name = document.form1.name.value;var email = document.form1.email.value;var password = document.form1.password.value;var errmsg = "";if(name == "") {errmsg += " - Name field cannot be blank\n";}if(email == "") {errmsg += " - Email field cannot be blank\n";}if(password == "") {errormsg += " - Password field cannot be blank\n";}if(errmsg != "") {alert(errmsg)}else{document.form1.submit()}</script>.... (continue with form) ...

I hope this helps you in a different kind of validation. PS im in a rish, will xplain some other time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.