dumb2champ Posted October 29, 2014 Share Posted October 29, 2014 Hello Guys...I need help about my validate scripts... $(document).ready(function(){ //Validation jQuery.validator.addMethod('validIPurl', function(value) { var ip = '^([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])$'; var URL = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/; //HOW TO JOIN AND VALIDATE BOTH VARIABLE IP AND URL???? } }, 'Invalid Address'); //if(/^(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i) $(".myfirstform").validate( { rules: { ip: { validIPurl: true } }, ); }, I use jquery.validate javascripts to validate user input.. I also try to create if..else to join IP and URL but the control statement not working Hope you guys can help for the solution... Quote Link to comment https://forums.phpfreaks.com/topic/292146-input-validation-in-jqueryjs/ Share on other sites More sharing options...
Alex_ Posted November 1, 2014 Share Posted November 1, 2014 (edited) Haven't used jquery.validate myself but from your snippet I'm assuming jquery.validate is expecting a simple bool return value, so you should be able to just match your regularexpressions in the validIPurl function. Something like jQuery.validator.addMethod('validIPurl', function(value) { var ip = ^([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]).([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]); var URL = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/; var ipMatcher = new RegExp(ip); var urlMatcher = new RegExp(url); return ipMatcher.test(value) || urlMatcher.test(value); } }, 'Invalid Address'); Assuming I understood what you meant. It will accept either a valid IP, or a valid URL. Edited November 1, 2014 by Alex_ Quote Link to comment https://forums.phpfreaks.com/topic/292146-input-validation-in-jqueryjs/#findComment-1495459 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.