How to check validation of IP Address in jquery

前端 未结 9 1761
挽巷
挽巷 2021-01-17 21:39

I need to add IP Validation in my project .Is there any function in jquery or in jquery mobile.So that it will validate the in put field?

Thanks

相关标签:
9条回答
  • 2021-01-17 22:16

    This should work for IP address

    $.validator.addMethod('IP4Checker', function(value) {
    
        var ip="^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
            return value.match(ip);
        }, 'Invalid IP address');
    
        $('#remoteForm').validate({
            rules: {
                ipAddr: {
                    required: true,
                    IP4Checker: true
                }
            }
        });
    
    0 讨论(0)
  • 2021-01-17 22:19

    refer this document IP validation

    here he has used jqueryvalidator.js and explained with example.

     $.validator.addMethod('IP4Checker', function(value) {
                var ip = "^(?:(?:25[0-5]2[0-4][0-9][01]?[0-9][0-9]?)\.){3}" +
                    "(?:25[0-5]2[0-4][0-9][01]?[0-9][0-9]?)$";
                    return value.match(ip);
                }, 'Invalid IP address');
    
                $('#form1').validate({
                    rules: {
                        ip: {
                            required: true,
                            IP4Checker: true
                        }
                    }
                });
    
    0 讨论(0)
  • 2021-01-17 22:21

    The short version:

    ^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$
    

    Explained here https://stackoverflow.com/a/26445549/3356679

    0 讨论(0)
提交回复
热议问题