| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Senior Member | 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 |
| |
| | #2 (permalink) |
| NPQ's PA, Slave, and On Call Coder Technical Services | 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).
__________________ |
| |
| | #3 (permalink) | ||
| Senior Member | Hello, thanks for this..but there's something I'm doing wrong? I put the code as : Quote:
Quote:
| ||
| |
| | #4 (permalink) |
| NamePros Regular | 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)) { |
| |
| | #7 (permalink) |
| Senior Member ![]() | 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 |
| |
| | #8 (permalink) |
| NamePros Regular | What about something like this? This seems to be a very simple script: http://www.codetoad.com/javascript_f...ion_script.asp |
| |
| | #10 (permalink) |
| Account Closed | 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>
"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. |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
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 |