Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  51] [ 1]  / answers: 1 / hits: 36518  / 9 Years ago, fri, september 18, 2015, 12:00:00

I've searched and tried just about everything and nothing works. I'm just trying to have this happen:



onclick a submit button --> check to see if a text field is empty --> if it is, alert an error and do not advance to the next page (if not, advance but only if the values are between 0 - 100 numerically.



Here's what I've got right now (other code I've found that works):



<form name=agencytrial>
<div class=textcenter>
<input name=AgencyRating id=AgencyField type=text value= autocomplete=off class=forceNumeric onchange=handleChange(this); />
<input class=button id=FormSubmitButton type=submit value=Submit onclick=checkField(); />
</div>
</form>

<script>
function handleChange(input) {
if (input.value < 0) alert(Value should be between 0 - 100);
if (input.value > 100) alert(Value should be between 0 - 100);
}
</script>


The above code works fine, and if someone types something into the field, and if it is not between 0 - 100, then it will alert and error and NOT advance the page.



But if no one types anything, they can just click the submit button and advance the page, and the above code does nothing to stop that. So I tried to use something like the following (one of many attempts):



<script type = text/javascript>
function checkField(){
var x = document.getElementById(AgencyField);
if (var x = ){
alert(Value should be between 0 - 100);
return false;
}else{
return true;
}
</script>


The above code fails, and they can just click the submit button and advance without typing anything into the field.



Any help would be majorly appreciated!



------------EDIT--------------



Based on everyone's comments, I've rewritten my code and it's very close. BUT HERE'S WHAT I DON'T GET:



When I input number > 100, it alerts message and DOES NOT ADVANCE.



When I leave it blank, it alerts message and ADVANCES.



Why the difference?



<form name=agencytrial>
<div class=textcenter>
<input name=AgencyRating id=AgencyField type=text value= autocomplete=off class=forceNumeric onchange=handleChange(this)/>
<input class=button id=FormSubmitButton type=submit value=Submit onclick=handleChange()>
</div>
</form>

<script>
function handleChange(input){
var x = document.getElementById(AgencyField).value;
if (x < 0 || x > 100 || x === ) alert(Value should be between 0 - 100);
}
</script>


-------EDIT #2--------



Here's all my code below. I am just using form onsubmit and the onclick codes now, as the onchange code was not going to stop the key or the button from being clicked with the form being empty (i.e., never modified).



I've got the alert popping up with either or the button being clicked, but the page always advances after I close the alert.



<?php
$compTime = 8; // time in seconds to use for 'computer' timing
if ($text === '') { $text = 'How likely was it that you caused the tone to occur?|Type your response on a scale from 0-100.'; }
$texts = explode('|', $text);
$mainText = array_shift($texts);
?>
<!DOCTYPE html>
<html>
<form onsubmit=return handleChange();>
<div class=textcenter>
<h3><?php echo $mainText; ?></h3>
<?php
foreach ($texts as $t) {
echo '<p>' . $t . '</p>';
}
?>
</div>

<div class=textcenter>
<input name=AgencyRating id=AgencyField type=text value= autocomplete=off class=forceNumeric/>
<input class=button id=FormSubmitButton type=submit value=Submit onclick=return handleChange()/>
</div>
</form>

<style type=text/css>
img {
display: block;
position: relative;
bottom: -40px;
left: -21px;
max-width:520px;
max-height:520px;
width: auto;
height: auto;
}
</style>

<body>
<img src=/tapa/Experiment/images/scale.jpg</img>
</body>
</html>


<script type=text/javascript>
function handleChange(input) {
var x = document.getElementById(AgencyField).value;
if (x === || x < 0 || x > 100) {
alert(Value should be between 0 - 100);
return false;
}
return true;
}
</script>


*****EDIT*****



I have solved the problem using a new script. Thanks to everyone and the solutions provided would usually work, but just not in my case for reasons not entirely clear to me but not worth explaining.


More From » javascript

 Answers
13

Updated question:




When I input number > 100, it alerts message and DOES NOT ADVANCE.




When you input a number > 100 and click submit, it will trigger the onchange. It won't even trigger the submit button.




When I leave it blank, it alerts message and ADVANCES.




It advances because you don't tell it not to. To do that, you have to return false on the onclick event (see fix below).



Fix:



Add return to #FormSubmitButton's onclick:



<input class=button id=FormSubmitButton type=submit
value=Submit onclick=return handleChange()>
^^^^^^-- add this


Add returns to the JavaScript function:



function handleChange(input) {
var x = document.getElementById(AgencyField).value;
if (x < 0 || x > 100 || x === ) {
alert(Value should be between 0 - 100);
return false;
}
return true;
}


Check updated demo fiddle here.






Original question



Several issues:



var x = document.getElementById(AgencyField);


This way, x only gets a reference to the #AgencyField element. You don't want that, you want its value: var x = document.getElementById(AgencyField).value;



Also, you are doing an assignment in your if, not a comparison:



if (var x = ){


This line should be if (x === ){.



Your function checkField() { also is missing an ending }.



Most important, and subttle, is:



<input class=button id=FormSubmitButton type=submit value=Submit onclick=checkField(); />


You should have a return there (also remove that ;):



<input class=button id=FormSubmitButton type=submit value=Submit onclick=return checkField()/>


All in all, check an updated working fiddle here. There are more changes, they all are shown below (in the comments).



Fixed JavaScript:



function handleChange(input) {
if (input.value.length < 0) { // added .length
alert(Value should be between 0 - 100);
return false; // added this
}
if (input.value.length > 100-1) { // added .length AND added -1
alert(Value should be between 0 - 100);
return false; // added this
}
return true; // added this
}

function checkField() {
var x = document.getElementById(AgencyField).value; // added .value
if (x === ) { // if (var x = ) -> if (x === )
alert(Value should be between 0 - 100);
return false;
} else {
return true;
}
} // added this }


Fixed HTML:



<form name=agencytrial>
<div class=textcenter>
<input name=AgencyRating id=AgencyField type=text value= autocomplete=off class=forceNumeric onkeypress=return handleChange(this) ; />
<input class=button id=FormSubmitButton type=submit value=Submit onclick=return checkField() />
</div>
</form>
<!-- Changes to HTML elements:
#FormSubmitButton
- removed ; at the end
- added return to onclick
#AgencyField
- changed onchange to onkeypress
- added return
-->

[#65005] Thursday, September 17, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamila

Total Points: 490
Total Questions: 94
Total Answers: 94

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;