[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 06-28-2005, 07:57 AM   #1 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Javascript needed. (Np$+rep+tr)

Hello,

I'm not a JS fan, but I have to use JS somewhere on my site.. well here's what I need.

I need an easy javascript that will check my form to find any empty fields that user has left blank, and if any occur, it gives him an alert to fill in this or that. "form validation".

One thing I'm facing here, this code must work on all browsers. So this is the only requirement.

I'll pay 100np, and I'll also add to your TR/Rep. If you could help me with this code.

I do not care whether you're the writer of the code or not. I just need it to work on all browsers. And please check that it does, because it's very important to my site.

Thank you

Porte is offline  
Old 06-28-2005, 08:21 AM   #2 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services


 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,545
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
Brought to you by www.javascriptkit.net I tried it in Both IE and FF. And it worked.

Code:
//Step 1:Insert the below into the <head> section of your page:
<script>

/*
Check required form elements script-
By JavaScript Kit (http://javascriptkit.com)
Over 200+ free scripts here!
*/

function checkrequired(which){
var pass=true
if (document.images){
for (i=0;i<which.length;i++){
var tempobj=which.elements[i]
if (tempobj.name.substring(0,8)=="required"){
if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==-1)){
pass=false
break
}
}
}
}
if (!pass){
alert("One or more of the required elements are not completed. Please complete them, then submit again!")
return false
}
else
return true
}
</script>

//Step 2: Add the following inside the <form> tag of the form you wish to validate:

<form onSubmit="return checkrequired(this)">
--
--
</form>

//Specifying required elements: Now, to mark an element inside the form as "required", //simply prefix the element's name with the keyword "required". Uh? For example, using //the above example, the source code looks something like this:

<form onSubmit="return checkrequired(this)">

<input type="text" name="requiredname">
<input type="text" name="requiredemail">
<select name="requiredhobby">
<option>....</option>
</select>
<textarea name="comments"></textarea>

</form>

//Notice how the names of all of the required elements have one thing in common- //their names have the prefix "required". This indicates to the script that those are //elements that have to have input in them. For either text or textarea elements, this //means they have to contain some text; for selection list, this means at least one //selection has to be selected.

//Note: Due to the "optional" nature of check and radio boxes, this script cannot be //used to mark those elements as required (giving check or radio boxes a "required" //prefix will do nothing).
__________________
Eric is offline  
Old 06-28-2005, 08:40 AM   #3 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Hello, thanks for this..but there's something I'm doing wrong?

I put the code as :

Quote:
<html>
<head>
<script>

function checkrequired(which){
var pass=true
if (document.images){
for (i=0;i<which.length;i++){
var tempobj=which.elements[i]
if (tempobj.name.substring(0,8)=="required"){
if (((tempobj.type=="text"||tempobj.type=="textarea") &&tempobj.value=='')||(tempobj.type.toString().cha rA t(0)=="s"&&tempobj.selectedIndex==-1)){
pass=false
break
}
}
}
}
if (!pass){
alert("One or more of the required elements are not completed. Please complete them, then submit again!")
return false
}
else
return true
}
</script>

</head>
<body>

<form onSubmit="return checkrequired(this)">

<input type="text" name="requiredname">
<input type="text" name="requiredemail">
<select name="requiredhobby">
<option>....</option>
</select>
<textarea name="comments"></textarea>
<input type="submit" value="submit">
</form>
</body>
</html>
but then I receive this error:

Quote:
Line: 11
Char: 108
Error: Expected ')'
Code: 0
URL: http://localhost/test.php
Porte is offline  
Old 06-28-2005, 10:35 AM   #4 (permalink)
NamePros Regular
 
Join Date: Jun 2005
Posts: 265
270.00 NP$ (Donate)

Simsi will become famous soon enoughSimsi will become famous soon enough


At a quick glance i'd suggest putting spapaces in this line:

if (((tempobj.type=="text"||tempobj.type=="textarea") &&tempobj.value=='')||(tempobj.type.toString().cha rA t(0)=="s"&&tempobj.selectedIndex==-1)){

To read:

if (((tempobj.type=="text" || tempobj.type=="textarea") && tempobj.value=='') || (tempobj.type.toString().charA t(0) == "s" && tempobj.selectedIndex==-1)) {
Simsi is offline  
Old 06-28-2005, 11:20 AM   #5 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Thanks, let me try it..

edit: It does not solve it.. still same error

Last edited by Designporte; 06-28-2005 at 11:25 AM.
Porte is offline  
Old 06-28-2005, 11:50 AM   #6 (permalink)
NamePros Regular
 
Join Date: Jun 2005
Posts: 265
270.00 NP$ (Donate)

Simsi will become famous soon enoughSimsi will become famous soon enough


Quote:
Originally Posted by Designporte
Thanks, let me try it..

edit: It does not solve it.. still same error
Got a URL handy where i can see it DP?
Simsi is offline  
Old 06-28-2005, 12:15 PM   #7 (permalink)
Senior Member
 
Join Date: Apr 2005
Location: .ma.us
Posts: 1,783
21.95 NP$ (Donate)

DropLister is a splendid one to beholdDropLister is a splendid one to beholdDropLister is a splendid one to beholdDropLister is a splendid one to beholdDropLister is a splendid one to beholdDropLister is a splendid one to beholdDropLister is a splendid one to beholdDropLister is a splendid one to behold

Baby Health Save a Life Save The Children
try something like <script type='javascript'> I think you need to specify in that tag thats its javascript and not java or something...just looked it up try adding this <SCRIPT LANGUAGE="JavaScript"> instead of just script, it miught alter how the browser reads it, but i dont do js so....gl
__________________
DropLister is offline  
Old 06-28-2005, 12:19 PM   #8 (permalink)
NamePros Regular
 
gamex's Avatar
 
Join Date: Feb 2004
Location: Student @ UConn
Posts: 408
73.95 NP$ (Donate)

gamex has a spectacular aura aboutgamex has a spectacular aura about


What about something like this? This seems to be a very simple script:

http://www.codetoad.com/javascript_f...ion_script.asp
__________________
Joe
CarrotCash.com
gamex is offline  
Old 06-28-2005, 01:31 PM   #9 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Well, this script doesn't let me specify optional fields, it will just occur on all input fields.
Porte is offline  
Old 06-29-2005, 12:10 AM   #10 (permalink)
Account Closed
 
Join Date: May 2005
Location: NY, USA
Posts: 285
45.00 NP$ (Donate)

hasif has a spectacular aura abouthasif has a spectacular aura about


Thumbs up

This is a code untill the require fields are filled submit button will be DIM after all the fields are filled the button will be active to submit.

Code:
<html>
<head>

  <!-- Required Changes to use Form Set Validation:

    1. var form_nm  = "myForm"   :: This declares the form name to validate.
    2. set="[optional,required]" :: a value of required makes it valid only if it matches the regexp.
    3. regexp="/^[regex here]$/" :: only needed for required elements.  Element is valid if it tests true to the regular expression.

    ==> To validate for a minal length:  /^.{#,}$/ :: where # represents the minimum length

  -->

  <style type="text/css">

    .fld {
      width:285px;
      text-align:center;
    }

    div#errors {
      color:red;
      font-weight:bold;
    }

  </style>

  <script type="text/javascript">


    /***************************************************
    * Original:    Hasif Ahmed
    * WebSite:     http://masterzone.net.tf
    * Email:       hasif5@gmail.com

    * Name:        From Set Validator
    * Description: Validates a given form by detecting
                   > its set and testing against its
                   > regular expression. All browsers pass.
    ***************************************************/

    var required = new Array();
    var form_nm  = "myForm";

    window.onload = function() {
      var elems = document.forms[form_nm].elements;
      for(var i=0,a;a=elems[i];i++) {
        if(a.getAttribute('set') == "required") {
          required[required.length] = i;
          a.onkeyup = function() {
            return isOk(this.form);
          }
        }
      }
      return isOk(document.forms[form_nm]);
    }

    function isOk(frm) {
      var error = new Array();
      for(var i=0,a;i<required.length;i++) {
        a=required[i];
        if(!eval(frm.elements[a].getAttribute('regexp')).test(frm.elements[a].value)) {
          error[error.length] = "Field "+ frm.elements[a].getAttribute('id') +" are not correctly filled in.";
        }
      }
  
      frm.elements[frm.elements.length-1].disabled = error.length;
      document.getElementById('errors').innerHTML = "Good to Go!";

      if(error.length) {
        document.getElementById('errors').innerHTML = error.join('<BR>');
      }
    }

  </script>
</head>

<body>
  <form name="myForm" action="someScript.pl" method="post">
    <fieldset class="fld"><legend>Fields: </legend>
      <!-- Validates with at least 1 digit, only digits -->
      Numbers: <input type="text" id="numbers" set="required" name="txt1" regexp="/^\d+$/"><BR>
      <!-- Optional -->
      Text2:   <input type="text" id="txt2"    set="optional" name="txt2"><BR>
      <!-- Optional -->
      Text3:   <input type="text" id="txt3"    set="optional" name="txt3"><BR>
      <!-- Validates with 5 letters, or more, no non-letters -->
      Letters: <input type="text" id="letters" set="required" name="txt4" regexp="/^[A-Za-z]{5,}$/"><BR>
      <button type="submit" name="sbmt" id="sbmt" disabled="true">Submit!</button>
    </fieldset>
  </form>

  <fieldset class="fld" style="height:100px;"><legend>Errors</legend>
    <div id="errors">
      Any errors will be posted here.
    </div>
  </fieldset>
</body>
</html>
Good luck, if its work then tell me, I have tried in the following browsers :-
"FireFox" , "DeepAprk Alpha" , "Opera" , "Internet Explorer" , "MSN Explorer" , "Avant Browser" & "Netscape".
^It works fine in this browsers^

Last edited by hasif; 06-29-2005 at 12:16 AM.
hasif is offline  
Old 06-29-2005, 12:22 AM   #11 (permalink)
Senior Member
 
dotcommakers's Avatar
 
Join Date: Oct 2003
Location: world wide web
Posts: 2,022
1,279.01 NP$ (Donate)

dotcommakers is a splendid one to beholddotcommakers is a splendid one to beholddotcommakers is a splendid one to beholddotcommakers is a splendid one to beholddotcommakers is a splendid one to beholddotcommakers is a splendid one to beholddotcommakers is a splendid one to beholddotcommakers is a splendid one to behold


if u give me just a form.. i will put there java script so it will work for sure 100%
dotcommakers is offline  
Old 06-29-2005, 02:47 AM   #12 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Thanks guys, I'm not looking anymore..

Oh, well i have found it fair to share resources between both of you, thanks for the contributions.. and I think i found what i need.
Porte is offline  
Old 06-29-2005, 05:55 AM   #13 (permalink)
Account Closed
 
Join Date: May 2005
Location: NY, USA
Posts: 285
45.00 NP$ (Donate)

hasif has a spectacular aura abouthasif has a spectacular aura about


no problem, i good luck.
hasif is offline  
Old 06-29-2005, 02:55 PM   #14 (permalink)
NamePros Member
 
Join Date: Oct 2003
Posts: 126
251.90 NP$ (Donate)

i386 is an unknown quantity at this point


I think you could verify it on the script side with empty() in PHP.
i386 is offline  
Old 06-30-2005, 02:59 AM   #15 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Yes, I could, but I want the whole verification to go before submitting the page.
Porte is offline  
Old 06-30-2005, 03:27 AM   #16 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


point is though, if the user of your site dosen't have javascript enabled, or a browser which can't run javascript, you are stuck.
PoorDoggie is offline  
Old 06-30-2005, 03:57 AM   #17 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Yes Tom, heh..but I have considered that all users have JS
Porte is offline  
Old 06-30-2005, 04:03 AM   #18 (permalink)
Soon to be RICHdoggie!
 
PoorDoggie's Avatar
 
Join Date: Jan 2005
Location: UK
Posts: 2,390
316.50 NP$ (Donate)

PoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nicePoorDoggie is just really nice


Quote:
Originally Posted by Designporte
Yes Tom, heh..but I have considered that all users have JS
well then, your sorted. I was just pointing out that some people won't have js support. Sorry
PoorDoggie is offline  
Closed Thread

« md5 table | MYsql »

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

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB6 PROGRAMMER NEEDED will pay in NP$..or $ easy work jane118 For Sale / Advertising Board 1 12-07-2004 05:37 PM
Game - Minimum 100 NP$ in prizes - FREE TO PLAY ppx Contests Forum 25 12-06-2004 04:27 PM

Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 05:00 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85