Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form check if tow numbers are the same 1

Status
Not open for further replies.

davmold

IS-IT--Management
Jul 16, 2007
29
US
hi,
I am working on creating a form. I have about 16 fields where the user need to enter a number. But I need to make sure that before the form is submited I can check that the user has not enter the same number twice and that is less than 100. I am new to programming and looking for some pointers in how to get started or some examples that I could use.

Thank
 
I was able to find the following script but not sure how to add more fields this only compares 2 fields
Code:
<script type="text/javascript" language="JavaScript">
<!--

function BothFieldsIdenticalCaseSensitive() {
var one = document.form1.one.value;
var another = document.form1.two.value;
if(one != another) { return true; }
alert("Oops, both fields must be identical.");
return false;
}

function BothFieldsIdenticalCaseInsensitive() {
var one = document.form1.one.value.toLowerCase();
var another = document.form1.two.value.toLowerCase();
if(one != another) { return true; }
alert("Oops, both fields must be identical.");
return false;
}

//-->
</script>
 
are there other fields on the form, or just the 16 text boxes? any buttons, checkboxes, drop-downs?

are all of your text boxes for this numeric entry?

if so, you can loop through all text fields on the form and do your testing there.

try something like this (untested):

Code:
var e = document.forms['myFormName'].elements;
var strUsed = "";
for ( var i = 0; i < e.length; i++ ) {
    if ( e[i].type == "text" ) {
        if ( parseInt(e[i].value) >= 100 ) {
            alert('must be less than 100');
            return false;
        }
        if ( strUsed.indexOf(e[i].value + ":") > -1 ) {
            alert('number already used');
            return false;
        }
        strUsed += ( e[i].value + ":" );
    }
}

call from the form's onsubmit event.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
cLFlaVA,

there are 16 text boxes and only numbers will be entered.

in your example I would only have to change the form name and do something with the submit?

would that be like on submit= document.forms?
 
again, i didn't test that, so i have no clue if it will work. you'll likely need to add some code to verify that they're entering numbers, but this should get you started.

yes, change "myFormName" to the actual name of your form. then, call the function like this:

Code:
<form name="..." ... onsubmit="return myFunc();">

this is assuming you name your function "myFunc", as below:

Code:
function myFunc() {
    var e = document.forms['myFormName'].elements;
    var strUsed = "";
    for ( var i = 0; i < e.length; i++ ) {
        if ( e[i].type == "text" ) {
            if ( parseInt(e[i].value) >= 100 ) {
                alert('must be less than 100');
                return false;
            }
            if ( strUsed.indexOf(e[i].value + ":") > -1 ) {
                alert('number already used');
                return false;
            }
            strUsed += ( e[i].value + ":" );
        }
    }
}



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
thank you so very much clFlaVA I really, really appreciate the help. I was able to get it going. looks great!!!!
 
before I go could I add a on focus to the script?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top