tags:

views:

545

answers:

4

Hi I have created a registration form(sample one fro trial). But if i dont give the value in the field,still it submit. So I need to validate the field using java script and have to give a message"please fill the form" if the field is kept empty. this is my code

<html>
<body>
<form name="reg1" action="try.php" method="post">
<table>
<table border="0" cellpadding="0" cellspacing="0" width="779" align="centre">
<tr>
<th>Registration form</th></tr>
</tr>
<tr><TD align="left">username<TD/>
<TD><input type="text" name= "usr"></TD></tr>

<tr>
<td align="left">address1<td/>
<td><input type="text" name="addr1"></td>
</tr>

<tr>
<td align="left">address2<td/>
<td><input type="text" name=addr2></td></tr>
<tr>
<td align="left">password<td/>
<td><input type="password" name="pswd"></td>
</tr>
<tr><TD></TD>
<td><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>

can please anyone help????

+1  A: 

I would first write the server-side validation so that it works even is JS is unavailable. Add the client-side validation afterwards.

In PHP, you could check the $_GET / $_POST arrays to see if the desired values are present, and if not, trigger the output of some error messages when you output your form HTML.

Paul Dixon
but how. I am new to php and java script. can you plz suggest me how to do this
Google for php form tutorial and you will find lots of examples
Paul Dixon
ok let me check. and can you please suggest any good tutorials for javascript used in php
again, learn how to learn! Google for "javascript form validation" and come back with a more specific question :)
Paul Dixon
+1  A: 

You can find the howto here.

However, you will have to use server side validation too in the production system, JS validation can be easily bypassed.

slipbull
A: 

PHP is server-side; if you wish to prevent submission to the server, then you will need to do client side validation with Javascript. In addition to that, it is a good idea to validate on the server side because Javascript can be turned off or by-passed. Client side validation helps to take the load of validation off the server and making the registration more user-friendly, but you would still need server-side validation for safety.

Extrakun
how we can do the server side validation? I am new to this. I am sorry if it was stupid question
A: 

If you want to do server-side validation, I would do it on the page that I'm sending the post request to. In your case this would be try.php. Here's a small example in php:

$err=0;
if($_POST['phone']=='') {
   $err+=1;
}
if($_POST['email']=='' || !strstr($_POST['email'],'@')) {
   $err+=2;
}

if($err>0) {
   header("Location: firstpage.php?err=".$err);
}

//HTML starts here

I usually set it up to send an error code back to the first page if one of the fields is wrong. That can make things slightly more complicated, so you could leave that out if you don't need it.

DLH