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
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_
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.