Check IP format with Regular Expressions in JavaScript

by admin on July 24, 2009 · 0 comments

Here's a quick JavaScript function to check that a user has entered a valid IP address. Don't ask me why, but there may be times when you need a user to input an IP address, rather than just grab theirs. And it's always easier to check first on the client side with JavaScript than having to do all the validation on the post back to the server with PHP!

var filter = /^([1-9][0-9]{0,2})+.([1-9][0-9]{0,2})+.([1-9][0-9]{0,2})+.([1-9][0-9]{0,2})+$/;
         if (!filter.test(f.ip.value)) {
                alert('Please enter a valid IP');
                f.ip.focus();
                return false
         }

Previous post:

Next post: