Jump to content

Input Validation in Jquery/Js


dumb2champ

Recommended Posts

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... :)

Link to comment
https://forums.phpfreaks.com/topic/292146-input-validation-in-jqueryjs/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.