funbinod Posted December 9, 2014 Share Posted December 9, 2014 I tried to validate if a form input is within a php array or not using jquery. I tried the following <script>$('#date').blur(function(e) { var dt = $('#date').val(); var dtarr = <?php echo json_encode($edate); ?>; if(!($.inArray(dt, drarr))) { alert('NOT in range'); $('#date').focus(); }});</script> is what I did is right or not? or I must use ajax? please suggest... Link to comment Share on other sites More sharing options...
justsomeguy Posted December 9, 2014 Share Posted December 9, 2014 Does it work? When you load the page then PHP will write the JSON value there for whatever you have $edate set to at the time the page loads. Link to comment Share on other sites More sharing options...
funbinod Posted December 9, 2014 Author Share Posted December 9, 2014 no it doesn't work. I've set $edate as array with multiple values like ---- $edate= array ( 1 => '2014-07-17', 2 =>'2014-07-18', 3 =>'2014-07-19', 4 =>'2014-07-20', 5 =>'2014-07-21', 6 =>'2014-07-22', 7 =>'2014-07-23', 8 =>'2014-07-24', 9 =>'2014-07-25', 10 =>'2014-07-26', 11 =>'2014-07-27', 12 =>'2014-07-28', 13 =>'2014-07-29', 14 =>'2014-07-30', 15 =>'2014-07-31', 16 =>'2014-08-01', 17 =>'2014-08-02', 18 =>'2014-08-03', 19 =>'2014-08-04', 20 =>'2014-08-05', 21 =>'2014-08-06', 22 =>'2014-08-07', 23 =>'2014-08-08', 24 =>'2014-08-09', 25 =>'2014-08-10', 26 =>'2014-08-11', 27 =>'2014-08-12', 28 =>'2014-08-13', 29 =>'2014-08-14', 30 =>'2014-08-15', 31 =>'2014-08-16', 32 =>'2014-11-20',); Link to comment Share on other sites More sharing options...
justsomeguy Posted December 9, 2014 Share Posted December 9, 2014 What does the Javascript code look like after PHP prints it? Are you getting Javascript errors in your console? Link to comment Share on other sites More sharing options...
funbinod Posted December 9, 2014 Author Share Posted December 9, 2014 hehe javascript console reminded me one typo error (I've declared var dtarr and returned drarr) but correcting it also doesn't solve the prob and console doesn't show anything now.... Link to comment Share on other sites More sharing options...
justsomeguy Posted December 9, 2014 Share Posted December 9, 2014 So, what does the code look like? Link to comment Share on other sites More sharing options...
funbinod Posted December 9, 2014 Author Share Posted December 9, 2014 I got the solution now. I changed if(!($.inArray(dt, dtarr))) { to if(($.inArray(dt, dtarr)) == -1) { and it worked. thank u for the company Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now