Monday 15 July 2013

php - JavaScript validation using a for loop to check two or more form values are not the same -


I'm new to Javascript and have had some success with basic verification, but how can I work it out?

What is the form I have on the basis of which user selection can be between 1 to 10 drop down box, What is the value in my box recovered from my database is to ensure that The user has not selected more than one value before returning this data to his database.

The code given below has come with me so far. When the javascript function is run, it returns to the same page, but a warning appears on the site, but not in any popup box, as I used previously on other pages. It sees that values ​​have been selected more than once, but I have done something wrong because I get an undivided index error for php variables, which already had data.

  & lt; Script type = "text / javascript" & gt; & Lt ;! - var nov = "document.getElementById (" NOV ") .value == ''" ;; Var checkNov = new array (); CzechNo [] == 0; Function validate_reg () {while (nov> 0) {for (var veh = checknov) {if (document.getElementById ("FLT"). Value == '' == veh) {window.alert ("You assign Can not do more than one vehicle "); } Else {checknov [] = document.getElementById ("FLT"). Value == ''; }} Nov--; }} - & gt; & Lt; / Script & gt;   

I have added the variable name to the ID because it is defined as this form.

  & lt; Script type = "text / javascript" & gt; & Lt ;! - var nov = document.getElementById ("NOV"). Value == ''; Var checkNov = new array (); Checknov [] 0 ==; Function validate_reg () {while (neo> 0) {for (var veh = checknov) {if (document.getElementById ("FLT" nov) .value == '' == veh) {window.alert (" Can not assign more than one vehicle "); } Else {checknov [] = document.getElementById ("FLT" November) .value == ''; }} Nov--; }} - & gt; & Lt; / Script & gt;    

This code is awful ... First things first, you should not be valid Client Side! It is very easy to hang around, and as you can see already, if the verification is not working, the server side fights. All assumptions should be made to server side or both, if you prefer (it is possible to alert users before waiting time on server requests).

Nevertheless, you have innumerable flaws in your code, let's first go to them. First of all I am referring to the second code example, you provide a string on nov , when it appears that you intend to use a function explicitly. You have then checked a boolean on checknov that it is a syntax error that should be clear from browser warnings, but it will not do anything, even if it works!

Then you have a function that checks that nov is greater than zero ... but nov is a string similar to that comparison There is no point in! And then you try and perform for those loops that are completely invalid. There are three parts for loops: initialization, position of looping, and post-loop operation. You could have loop for a while, but your position is still invalid, because you are specifying the price rather than checking equality. I'm pretty sure what you want with the rest of your code.

I am sincerely, you have demonstrated that you do not know almost anything about Javascript. I recommend putting it down and learning JS.

But anyway, another correct method of recognition will be to use something that will return an array of dom objects in the class (and give it all the class of elements in that class) as a result, you choose your form Get the elements of the box (which you call the drop down box). You can then loop through this (remember that .length can be used to find the size of the array). However, you also need to understand how the loop works, and your code indicates that you do not.

Inside that loop, you have to loop again through all the elements, because you have to compare each element with all other elements

Edit: You should also indicate that you can get the value of an element.

Finally, I stress, just do not have client side evaluation .

EDIT2: Besides,.

No comments:

Post a Comment