Forum Moderators: open

Message Too Old, No Replies

form valdation

         

electricocean

11:36 pm on Sep 12, 2006 (gmt 0)

10+ Year Member



Hi, I am trying to validate my form and i am stuck. The form is created in php so I don't know how many field there so a i am giving a hidden variable.

My script:

<SCRIPT LANGUAGE="JavaScript1.2"> 
// Form object
var f=document.form.begining;
//Count Parts var
var parts;
var parts_array=new array();
// Boolen to track if error found
var foundErr;
// Form element index number which the first error occured.
var focusOn;

function check_form() {
foundErr = false; focusOn = -1;
//Count Parts
parts = d.parts.value;
for(var i = 1; i <= parts; i++){
var a_part="part"+i;
parts_array[i] = d.a_part.value;
}
//test if part vals are the same if they are print error
var track = new Array();
for(var i = 1; i <= parts; i++){
if(parts_array[i] == "Track #"){
track[i] = true;
}
}
var name = new Array();
for(var i = 1; i <= parts; i++){
if(parts_array[i] == "Name"){
name[i] = true;
}
}
var artist = new Array();
for(var i = 1; i <= parts; i++){
if(parts_array[i] == "Artist"){
artist[i] = true;
}
}
var album = new Array();
for(var i = 1; i <= parts; i++){
if(parts_array[i] == "Album"){
album[i] = true;
}
}
var skip = new Array();
for(var i = 1; i <= parts; i++){
if(parts_array[i] == "Skip Value"){
skip[i] = true;
}
}
var chooseid3_arr = array(1=>"track", "name", "artist", "album", "skip");
for(var i = 1; i <= parts; i++){
for(var j = 1; j <= count(chooseid3_arr); j++){
if(parts_array[j]!= "Skip Value"){
if(parts_array[j] = chooseid3_arr[j]){
if(count(array.parts_array[j]) < 2){
foundErr = false;
}
else{
alert(chooseid3_arr[j] +"is set to more than one value");
foundErr = true
}
}
else{
foundErr = false;
}
}
}

//Has any error occured?
if (foundErr) {
//Yes. Focus on which the first occurred.
for(i = 1; i<=parts; i++){
pull = "part" + i.value;
f.pull.value = "Skip Value";
}
} else {
// No. Submit the form.
f.submit.disabled = true;
f.submit();
}
}
</SCRIPT>

Sinisfun

5:03 pm on Sep 16, 2006 (gmt 0)

10+ Year Member



You say you are stuck, does it give any errors or does the page just hang? Can you provide some more info into what it is doing / not doing.

-Sin

electricocean

11:14 pm on Sep 25, 2006 (gmt 0)

10+ Year Member



nothing happens when I run it. it just continues to the next page

eelixduppy

12:29 am on Sep 26, 2006 (gmt 0)



View your browser's javascript errors. Also add alert() messages in the code to make sure that what is happening is actually what you want. Post any errors that you may receive.

Best of luck!

rocknbil

3:22 am on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




nothing happens when I run it. it just continues to the next page

Did you do this?

<form method="post" action="yourscript.cgi" onSubmit="return false;">

Or better yet as pointed out by RonPK: [webmasterworld.com]

<form onsubmit="return check_form()" action=[..]>

And make sure you alter your script so it returns false on error:

if (foundErr) {
//Yes. Focus on which the first occurred.
for(i = 1; i<=parts; i++){
pull = "part" + i.value;
f.pull.value = "Skip Value";
return false;
}

That being said, I must ask, are you making this task more difficult than it has to be? It indeed seems that alot of your repretive code may be easier managed by storing values in an array and looping through them.

At any rate, just FYI the modern script declaration is

<script type="text/javascript">