0

How to use a for loop in javascript to check for valid input?

I've tried my code but it does not work, how can I fix it?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<form onsubmit="return checkform(this);">
<span id="price_top"></span>
<a href="#" id="addScnt_price" style=" cursor: pointer; font-weight: bold; color: #0192B5; text-decoration: none; ">Add more</a>
<div id="p_scents_price">
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<input type="submit" name="submit" value="OK">
</form>

<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
var z = 1;
for(z=0;z<10;z++) {
  if ((form.price'+z+'.value != "") && (form.price'+z+'.value < "1.5")) {
    alert( "Minimum Price'+z+' $1.5" );
    document.getElementById("price_top").scrollIntoView()
    document.getElementById("price'+z+'").style.border = "1px solid red";
    return false ;
  }
  else {
    document.getElementById("price'+z+'").style.border = "1px solid #d5d5c5";
  }
}
return true ;
}
</script>

<script>
$(function() {
        var scntDiv = $('#p_scents_price');
        var i = 1;

        $('#addScnt_price').live('click', function() {
            $('<p><label for="p_scnts_price"><input type="text" id="price'+i+'" size="20" name="price[]"/><a href="#" id="remScnt_price">Remove</a></label></p>').appendTo(scntDiv);
                i++;
                return false;
        });

        $('#remScnt_price').live('click', function() { 
                if( i > 2 ) {
                        $(this).parents('p').remove();
                }
                return false;
        });
});
</script>

1 Answer 1

1

You can't open a string with " and terminate it with '. You had to review your element id refers. For example, you need to change:

document.getElementById("price'+z+'") ...

With:

document.getElementById("price"+z) ...

You also have to change this:

form.price'+z+'.value ...

With

eval("form.price"+z).value ...

eval function

Sign up to request clarification or add additional context in comments.

1 Comment

And how to apply loop for ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.