Minase Posted September 7, 2008 Share Posted September 7, 2008 hy there i do have an input box where i want to put a minimum value and a maximum one like the content should be just numeric so any other keys wont work,and the input to be based on min/max value example $min = 10 $max = 20 the input box value should be from 10 to 20 user will write it,is not a drop down box thank you Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 So... you want to make the input box only accept values between the $min and $max? Easy. <?php $msg = ""; if ($_POST['submit']) { $min = 10; $max = 20; $value = trim($_POST['value']); if (preg_match("/\b\d+\b/", $value)) $msg = "Number is not within range."; } echo "<form method='post'><input type='text' name='value' /><br />" . $msg; ?> How's that? Quote Link to comment Share on other sites More sharing options...
exally Posted September 7, 2008 Share Posted September 7, 2008 for javascript try something like <script> function checkValue() { var min = 10; var max = 20; var val = document.getElementById('inp').value; if(val < min || val > max) { alert('You have failed at inputting'); document.getElementById('inp').value = 10; } } </script> <input type="text" name="inp" id="inp" onchange="checkValue()" /> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 Though, I don't recommend JavaScript because of a few reasons: 1. The alert is very obtrusive when it pops up every time you change the value; 2. The if statement is not guaranteed without checking if the value contains numbers. Edit: Fast edit is annoying for all the special characters it puts into the box. <?php $msg = ""; if ($_POST['submit']) { $min = 10; $max = 20; $value = trim($_POST['value']); if (preg_match("/\b\d+\b/", $value)) $msg = "Number is not within range."; } echo "<form method='post'><input type='text' name='value' /><br />" . $msg; ?> Quote Link to comment Share on other sites More sharing options...
exally Posted September 8, 2008 Share Posted September 8, 2008 ken that requires the page to be reloaded everytime the user enters something. kinda annoying. the best would be to use an ajax script to do it and have a little <div> above the form with all form validation going through that. I just supplied the JavaScript he wanted. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 8, 2008 Share Posted September 8, 2008 ken that requires the page to be reloaded everytime the user enters something. kinda annoying. the best would be to use an ajax script to do it and have a little <div> above the form with all form validation going through that. I just supplied the JavaScript he wanted. Maybe, but it is less annoying than your onchange alert. If a number is 6 figures, I have one reload. Your onchange function can alert up to 6 times, or more if I made a typo. Now that would drive me nuts. Also, why would this require AJAX? ??? Quote Link to comment Share on other sites More sharing options...
exally Posted September 8, 2008 Share Posted September 8, 2008 i didn't say it was required, i said it would be the best... and yes after u pointed it out mine would be quite annoying... Quote Link to comment Share on other sites More sharing options...
Minase Posted September 8, 2008 Author Share Posted September 8, 2008 thank you ken but in PHP is easy to do,if i needed to do that in PHP,i would post it into php section. ofcourse after the JS part,i will use PHP also (JS is just client side,so better secure than vulnerable) thank you for your code ken exally from what i can see JS is not so hard,is similar to PHP. thank you your code is what i exactly needed. about the alert thing... its not good i will make it if $value < $min { // put the value at minimum, and with maximum also} so this is better but i will put it to do that just on mouseout,not when users write cause it is anoying thank you all Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.