Javascript needed. (Np$+rep+tr)

NamecheapNamecheap
Watch

Porte

VIP Member
Impact
145
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

:hearts:
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains — AI StorefrontUnstoppable Domains — AI Storefront
Brought to you by www.javascriptkit.net :D 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).
 
0
•••
Hello, thanks for this..but there's something I'm doing wrong?

I put the code as :

<html>
<head>
<script>

function checkrequired(which){
var pass=true
if (document.images){
for (i=0;i<which.length;i++){
var tempobj=which.elements
if (tempobj.name.substring(0,8)=="required"){
if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charA 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:

Line: 11
Char: 108
Error: Expected ')'
Code: 0
URL: http://localhost/test.php
 
0
•••
At a quick glance i'd suggest putting spapaces in this line:

if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charA 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)) {
 
0
•••
Thanks, let me try it..

edit: It does not solve it.. still same error
 
Last edited:
0
•••
Designporte said:
Thanks, let me try it..

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

Got a URL handy where i can see it DP?
 
0
•••
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
 
0
•••
0
•••
Well, this script doesn't let me specify optional fields, it will just occur on all input fields.
 
0
•••
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:       [email protected]

    * 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:
0
•••
if u give me just a form.. i will put there java script so it will work for sure 100%
 
0
•••
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.
 
0
•••
no problem, i good luck.
 
0
•••
I think you could verify it on the script side with empty() in PHP.
 
0
•••
Yes, I could, but I want the whole verification to go before submitting the page.
 
0
•••
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. ;)
 
0
•••
Yes Tom, heh..but I have considered that all users have JS :)
 
0
•••
Designporte said:
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
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back