473,506 Members | 16,954 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 1875
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
4246
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...
12
2437
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" ...
4
2612
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...
3
2842
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...
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...
9
4153
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...
27
4679
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...
11
2961
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...
10
5684
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...
2
4882
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.,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7021
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...
0
7478
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...
0
4701
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...
0
3188
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
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 ...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.