Jump to content
Sign in to follow this  
dumb2champ

Input Validation in Jquery/Js

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

Share this post


Link to post
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.

Edited by Alex_

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

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.