Current location: Hot Scripts Forums » General Web Coding » JavaScript » Form Validation


Form Validation

Reply
  #1 (permalink)  
Old 08-29-07, 06:25 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Form Validation

Hi!

I would like to do some validation on this section of the form

<input name="uploadedfile" type="file" />

i would like to do that such a way that user only need to upload file using Brows button, they can't write directly on the field, this field take input in general, but this has to be stop.

can any one help me in this regards?

the form already have validation on some field the code is as follows:

javascript Code:
  1. <script type="text/JavaScript">
  2. function validateform(){
  3.  sortir = 0;
  4.  if (document.form1.name.value == '') {
  5.   window.alert('The field Name is required. Please, be sure that you fill itm up.');
  6.   document.form1.name.focus();
  7.   sortir = 1;
  8.  }
  9.  
  10.  if (document.form1.email.value == '' && sortir == 0) {
  11.   window.alert('The fields Email is required. Please, be sure that you fill it up.');
  12.   document.form1.email.focus();
  13.   sortir = 1;
  14.  }
  15.  else
  16.  {
  17.     with (document.form1.email)
  18.   {
  19.   apos=value.indexOf("@");
  20.   dotpos=value.lastIndexOf(".");
  21.   lastpos=value.length-1;
  22.   if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
  23.    {
  24.    alert("Incorrect e-mail address");
  25.    return false;
  26.    }
  27.   }
  28.  }
  29.  if (document.form1.phone.value == '' && sortir == 0)
  30.  {
  31.   window.alert('The fields Phone is required. Please, be sure that you fill it up.');
  32.   document.form1.phone.focus();
  33.   sortir = 1;
  34.  }
  35.  else
  36.  {
  37.     var numericExpression = /^[0-9]+$/;
  38.  if(!(document.getElementById('phone').value.match(numericExpression)))
  39.  {
  40.   alert("Incorrect phone number");
  41.   document.getElementById('phone').focus();
  42.   return false;
  43.  }
  44.  }
  45.  
  46.  
  47.  if (sortir == 0){
  48.    document.form1.submit();
  49.  }
  50.  }
  51. </script>

Thanks in advance!

Last edited by Nico; 08-29-07 at 06:45 AM.
Reply With Quote
  #2 (permalink)  
Old 08-29-07, 01:40 PM
Wolf1994 Wolf1994 is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Its impossible. You cannot disable input box without disabling its function to file selection. Also its very difficult to check for correct file path - for example in Unix OS it may starts with "/home" instead of drive letter.
Reply With Quote
  #3 (permalink)  
Old 08-29-07, 09:12 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:
HTML Code:
<input name="uploadedfile" onclick="this.blur()" onkeydown="this.blur();this.value=''" onfocus="this.blur()" type="file" />
It won't stop anyone completely but it would surely annoy them hehe.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #4 (permalink)  
Old 08-30-07, 06:36 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Twod!

the whole code stop pressing brows button, but this code is perfect, no user input but you can select the file from brows button

HTML Code:
 
<input name="uploadedfile" onkeydown="this.blur(); this.value=''" type="file" />
 
Thanks again
Reply With Quote
  #5 (permalink)  
Old 09-02-07, 10:31 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
It stopped the browse button too? Strange, didn't do that when I tested. Good to know you found it useful tho. =)
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #6 (permalink)  
Old 09-05-07, 03:25 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sujata_ghosh View Post
the whole code stop pressing brows button, but this code is perfect, no user input but you can select the file from brows button
I think she meant, user cant input his values(by typing it) but he has to select a file only through browse button !
__________________
i am still a learner and i like $this-> smilie!
Reply With Quote
  #7 (permalink)  
Old 09-05-07, 08:05 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
If i use whole code its not allow me to press brows button nor typing something, which is not the aim.

so i change it little bit and now its allow me to press the brows button but not let me write anything by typing.

Thanks for your help and concern. strange things happens with me most of time, lol.
Reply With Quote
  #8 (permalink)  
Old 09-05-07, 02:35 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Oh, I think I know why the full code wouldn't let you push the button. The onclick event seems to catch clicks both on the button and the field, and I think I might have missed clicking the button when testing that event handler.
I was aiming to prevent pasting text by right-clicking and selecting paste. But I guess I should have added a check in the onclick handler to see which mouse-button (if any) was clicked and make an exception for button 1 so the Browse button can be clicked.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form Validation bally123 JavaScript 4 07-09-06 04:48 PM
Form Validation slimey PHP 10 03-20-06 06:23 PM
Need help with form validation script dathandawg JavaScript 1 12-29-05 12:17 AM
Form Validation - Prevent submit if fields are blank Cepeleon JavaScript 2 03-15-05 04:33 PM
Flexible form validation question epetoke JavaScript 6 09-12-04 04:19 PM


All times are GMT -5. The time now is 11:31 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.