473,569 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

form validation not working

27 New Member
im using java script for form validation..

here is the code

Expand|Select|Wrap|Line Numbers
  1. <script type="text/JavaScript">
  2.  
  3.  
  4.  function validate_required(field,alerttxt)
  5. {
  6. with (field)
  7.   {
  8.   if (value==null||value=="")
  9.     {
  10.     alert(alerttxt);return false;
  11.     }
  12.   else
  13.     {
  14.     return true;
  15.     }
  16.   }
  17. }
  18.  
  19. function validate_form(thisform)
  20. {
  21. with (thisform)
  22.   {
  23.   if (validate_required(email,"Email must be filled out!")==false)
  24.   {email.focus();return false;}
  25.  
  26.   }
  27.  
  28. }
  29.  
  30.  
  31.  
  32.  function valid(f) {
  33. if (!/^\d*$/.test(f.value)) {
  34. alert("Only integer numbers allowed!");
  35. f.value = f.value.replace(/[^\d]/g,"");
  36. }
  37. }
  38.  
  39.  
  40.  </script>

The script working fine with email validation and also the last one function is for contact no input field only takes numbers every thing is working fi9 now i want the code to work like if name field is empty it says fill name and so on for all input fields.

HTML FORM:
Expand|Select|Wrap|Line Numbers
  1. div id="forminfo"><form name="theform" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validate_form(this)" method="POST">
  2.     First Name:
  3.     *************<INPUT type="text" name=fname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  4.     <br>
  5.     <br>
  6.  
  7.     Last Name:
  8.    *************<INPUT type="text" name=lastname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  9.     <br>
  10.     <br>
  11.  
  12.     Email Address:*******<INPUT type="text" name=email style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  13.     <br>
  14.     <br>
  15.  
  16.     Address:
  17.     ******************<INPUT type="text" name=address style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  18.     <br>
  19.     <br>
  20.  
  21.     City:
  22.         **************************<INPUT type="text" name=city style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  23.     <br>
  24.     <br>
  25.  
  26.     Province:
  27.     *****************<INPUT type="text" name=province style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  28.     <br>
  29.     <br>
  30.  
  31.     Contact No:
  32.     ************<INPUT type="text" name=contactno maxlength="11"  onkeyup="valid(this)" style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center">
  33.     <br>
  34.     <br>
  35.     <br>
  36.        ***********************************************************************************<input type="submit" name="submit" value="submit" />
  37.  
  38.     </form>
May 7 '09 #1
2 1883
prabirchoudhury
162 New Member
Hey
this should work fine as form validation

Html form
Expand|Select|Wrap|Line Numbers
  1. <div id="forminfo">
  2.     <form name="theform" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return verify()" method="POST"> 
  3.     First Name: <INPUT type="text" name=fname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  4.     <br> 
  5.     <br> 
  6.  
  7.     Last Name: <INPUT type="text" name=lastname style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  8.     <br> 
  9.     <br> 
  10.  
  11.     Email Address:<INPUT type="text" name=email style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  12.     <br> 
  13.     <br> 
  14.  
  15.     Address:<INPUT type="text" name=address style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  16.     <br> 
  17.     <br> 
  18.  
  19.     City:<INPUT type="text" name=city style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  20.     <br> 
  21.     <br> 
  22.  
  23.     Province:<INPUT type="text" name=province style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  24.     <br> 
  25.     <br> 
  26.  
  27.     Contact No:<INPUT type="text" name=contactno maxlength="11"  onkeyup="valid(this)" style="HEIGHT: 19px; WIDTH: 174px" size="20" align="center"> 
  28.     <br> 
  29.     <br> 
  30.     <br> <input type="submit" name="submit" value="submit" /> 
  31.  
  32.     </form> 
  33. </div>

Java script

Expand|Select|Wrap|Line Numbers
  1. <script type="text/JavaScript">
  2.  function verify(){
  3.  
  4.     var digits = "0123456789";
  5.       var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
  6.       var numericExpression = /^[0-9]+$/;
  7.       var str=document.theform.email.value;
  8.         if(document.theform.fname.value==""){
  9.             alert("Please enter fname");
  10.             document.theform.fname.focus();
  11.         return false;
  12.         }else
  13.         if(document.theform.lastname.value==""){
  14.             alert("Please enter lastname");
  15.             document.theform.lastname.focus();
  16.         return false;
  17.         }else
  18.         if(!filter.test(str)){
  19.             alert("Please input a valid email address");
  20.            document.theform.email.focus();
  21.         return false;
  22.         }else
  23.         if(document.theform.address.value==""){
  24.             alert("Please input a valid  address");
  25.            document.theform.address.focus();
  26.         return false;
  27.         }else
  28.         if(document.theform.city.value==""){
  29.             alert("Please enter city");
  30.            document.theform.city.focus();
  31.         return false;
  32.         }else
  33.         if(document.theform.province.value==""){
  34.             alert("Please enter province");
  35.            document.theform.province.focus();
  36.         return false;
  37.         }else
  38.         if(document.theform.contactno.value==""){
  39.             alert("Please enter contact no");
  40.            document.theform.contactno.focus();
  41.         return false;
  42.         }else
  43.         if(!(document.theform.contactno.value.match(numericExpression))){
  44.             alert("Please enter number only\n contact no");
  45.            document.theform.contactno.focus();
  46.         return false;
  47.         }
  48.  
  49.  </script> 
May 8 '09 #2
gits
5,390 Recognized Expert Moderator Expert
first: wrap all your name-attribute values in double quotes ...
second: just retrieve the input fields with

Expand|Select|Wrap|Line Numbers
  1. var inpuptFields = document.getElementsByTagName('input');
then loop over the retrieved list and implement the required validation ... in case you have problems with this just show what you have tried so far regarding this problem

kind regards
May 8 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4249
by: TG | last post by:
Dear PHP Group, I have two forms that are used to collect user information. The first one takes user inputted values such as fullname, city, address etc. I want these values to display in the second form when it is called. Both forms are .htm files that call themselves when the submit button is press via the following command in each form:...
12
2447
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" HTML----------- Form is ----> <form action="thanks.php" method="post" name="contact_form" id="contact_form"> Name -------> <input type="text"...
4
2621
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a script that validates the form fields. the validation procedure is called ONCLICK event of the submit button. Follwowing is the structure of the...
3
2851
by: Skippytpe | last post by:
Does anyone have an idea why the form validation in the following page wouldn't be working? I had been using XHTML 1.0 transitional which allowed me to use the form attribute 'name.' I could then just point the regular expression test to document.login.frmEmployeeNumber.value and have it validate. Now that I'm at XHTML 1.1 strict, I can...
6
507
by: Darren | last post by:
I have a form that has 10 fields on it. I have made all of them "Required". I also am using vb if statements to decide whether or not each field should be on the page. I am using the vb to compare values in my database and if a certain field equals something then the field is shown on the form. When the form is displayed in the browser, it...
9
4160
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation...
27
4701
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it...
11
2977
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the...
10
5700
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the form the control is on? Basically I have a number of controls in a form that are required, and to check it I am setting the Validation Rule to...
2
4892
scubak1w1
by: scubak1w1 | last post by:
Hello, I am building a form that collects some data about a file and throws it into a PosgreSQL database and also allows the user to upload and process the file using PHP's $_FILES... i.e., <input type="file" name="file" id="file" size="50"/> Action is a PHP_SELF, method is POST, check with array_key_exists, etc... anyway..... :-) ...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.