Form Validation - Javascript

Pages: 1, 2
free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Form Validation - Javascript

vizskywalker
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?

Reply

dungsport
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!!!

Reply

dungsport
Oi, forgot. For the fields are ok with their data, remember to clear the messages.

<cell name>.innerHTML = "";

Reply

vizskywalker
Thank you, I will try that immediately.

Reply

vizskywalker
I can't tell if that works or not because the page keeps continuing on to the php script that is supposed to process the form. Here is the link: http://www.mouseisle.com/signup.html

Reply

dungsport
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://www.mouseisle.com/style.css" 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>

 

 

 


Reply

jipman
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.

Reply

vizskywalker
Thanks guys, the validation is working now (except I need to add the part to clear the marked portions when it is fixed if other problems appear). If you want to check it out, it is http://www.mouseisle.com/signup.html

Reply

soleimanian
QUOTE(vizskywalker @ Mar 27 2005, 02:20 AM)
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?
*



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://www.parsloyal.com/wallpaper/wallpaper.php

Reply

altieva
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.

CODE

<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.

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Pages: 1, 2
Similar Topics

Keywords : validation javascript

  1. Extplorer - A PHP -and JavaScript- based File Manager (7)
    Browsing the ExtJS examples website i found this excellent web-based file manager called
    eXtplorer . eXtplorer allows you to browse your webserver folders with an intuitive Layout which
    makes working with files very easy, and thanks to the great ExtJS Javascript Library you can drag
    & drop folders and files, filter directories and sort the file list using various criteria. You can
    use eXtplorer to for example: browse directories & files on the server. edit, copy, move, delete
    files. search, upload and download files. create and extract archives. create new fil...
  2. Send Php Variable To Javascript - (5)
  3. Xhtml Validation With Php In Cgi Mode - (0)
    Hi, Recently the host server of one of my clients change its PHP installation from the Apache mode
    to the CGI mode so because of this change i got some problems. For example, whatever page i view it
    shows these warnings at the top of the page: Warning: session_start() :
    open(/tmp/sess_b05e459fe625d81a303b59982be3da39, O_RDWR) failed: Permission denied (13) in
    /home/client_username/public_html/functions.php on line 9 Warning: session_start() : Cannot send
    session cache limiter - headers already sent (output started at
    /home/client_username/public_html/functions.php:9...
  4. Problem With Xhtml Validation - (6)
    Hi, i have a problem when i try to validate a php page that includes a litlle form and use sessions
    as a strict xhtml 1.1 page with the W3C XHTML VALIDATOR, the problem consists that everytime i send
    it to the validator it creates a hidden input with the PHP PHPSESSID in the form and also adds to
    the href attribute of every link this PHPSESSID, so if you have for example a link like this: CODE
    page with session var equal 1 it results in this: CODE page with session var equal 1
    and as you know it is recommended that always replace & with & to make yo...
  5. Need Help With Javascript In Php Urgently. - (4)
    If you look at: http://www.chessoscorner.com/tools/google_...p_generator.php You can see it
    produces an error like this: Parse error: syntax error, unexpected T_STRING in
    /home/chesso/public_html/tools/google_sitemap_generator.php on line 65 This particular section is
    not PHP code. The php TAG has been ended before my javascript code and reopened after my javascript
    code........ This page however works perfectly using WAMP5 locally on my pc (may have something to
    do with it using a newer version of PHP). Here is the javascript code in question: CODE   
    func...
  6. A Simple Checking & Validation PHP Script - (6)
    Hi, there is sometimes that you need to password protect a directory in your site but you dont have
    access to a database or you dont need it because only a few users will access this directory, well
    the following script i develop will help in this situation. With only 2 files you can implement a
    basic security, the first file is a simple txt file where you store your users information and the
    second file is the php script. You can name the files whatever you want and can be used in any site
    with php support. The users.txt file: In this file simply put one line at the...
  7. How Do I Do Script Checking & Validation In PHP? - (17)
    I'm totally new to all this, and I've just done a registration/login script. It has 5
    different files that I put stuff in, plus a database. When I try to see how it looks on my website,
    well, it just doesn't show up. What I want to know is, how can I tell what I'm doing wrong?
    I don't always understand what the editors in the script programs are trying to say. Is there
    anyone out there who can help? My scripts are written in PHP with a MySql database. Boy, this all
    gets confusing, but I sure love every minute of it!...
  8. Simple E-mail Validation - check for correct address and syntax (2)
    Hey , this tutorial will tell you a very simple way to check if email addresses entered are with the
    correct syntax. Hope this will help you eliminate jokers. So this script will... (check the
    characters, and check if there is a " someone somewhere.extension ". Ok, so firstly we create a
    function with all the invalid stuff in it:- CODE function CheckMail($email) { if (eregi("^ (
    ? )*@ ( ? )*\. {2,4}$", $email)) { return true; } else { return false; } } Now, to check an
    email you just run the variable with the email in it through the function as foll...



Looking for form, validation, javascript






*SIMILAR VIDEOS*
Searching Video's for form, validation, javascript
advertisement




Form Validation - Javascript



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE