IP Validation on QlineEdit



  • I have 5 Qline Edit which accept diff diff Ip address and one push button.
    whenever i clicked pushButton i want to verify QlineEdit text is correct or not (with ip validator)

    QString IpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
    QRegExp IpRegex ("^" + IpRange
    + "(\." + IpRange + ")?"
    + "(\." + IpRange + ")?"
    + "(\." + IpRange + ")?$");
    QRegExpValidator *ipValidator = new QRegExpValidator(IpRegex, this);

    uiPoMo->lineEdit_ServerIP_2->setValidator(ipValidator);
    

    I used this validation But when i entered only 1 integer it was accept how? its not proper working?

    if(false == uiPoMo->lineEdit_ServerIP_2->hasAcceptableInput())
    {
    QMessageBox::warning(this,"Waring!"," not valid");
    }
    else
    {
    QMessageBox::warning(this,"Waring!"," valid");
    }

    When Pushbutton pressed this code Execte but not work properly?
    can any one Help Me ?

    Thank You.



  • @Pranit-Patil
    This may or may not solve you problem, but:

    "(\."

    In a C++ literal string, don't you mean:

    "(\\."

    ?



  • @Pranit-Patil Hi there.

    Like @jonB says above, you should to replace\.to\\.to avoid an error of escape caracter.

    But if you want to validate just if the user insert the complete ip, you must to remove the "optional counter caracter" of each group of numbers.

    Example:

    "(\\." + IpRange + ")?"
    

    this sentence accepts:
    (void) and 0 until 255 number because of ? caracter that means: 0 or 1 time

    NOTE: QRegExp is an old class of Qt4, if you are using Qt5+, i suggest you to use QRegularExpression

    Below is a code solving the problems:

        QString IpRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])";
        QRegularExpression IpRegex ("^" + IpRange
                                    + "(\\." + IpRange + ")"
                                    + "(\\." + IpRange + ")"
                                    + "(\\." + IpRange + ")$");
        QRegularExpressionValidator *ipValidator = new QRegularExpressionValidator(IpRegex, this);
    


  • Thank you for your guidlines.
    yesterday only i solved my Problem

    Thank You.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.