Asp.net C ++ No: digit and validity check

Source: Internet
Author: User

# Ifndef _ ct3d _ checkvalidval _ h __
# Define _ ct3d _ checkvalidval _ h __

Class cstringex
{
Private:
Cstring m_str;
Int m_dot_count;
 
Public:
Cstringex ()
{
M_dot_count = 0;
}
Cstringex (const cstring & s)
{
M_str = s;
}
Virtual ~ Cstringex ()
{
}

Public:
Bool isfloat ()
{
If (m_str.isempty ())
Return false;

M_dot_count = 0;

Int start_pos = 0;
Char c = m_str.getat (0 );
If (c = '+' | c = '-')
Start_pos = 1;

Int length = m_str.getlength ();
For (int I = start_pos; I <length; I ++)
{
C = m_str.getat (I );
If (c = '.')
{
M_dot_count ++;
Continue;
}

If (c <0x30 | c> 0x39)
Return false;
}

If (m_dot_count> 1) //... 23.
Return false;
If (length = 1 & (start_pos = 1 | m_dot_count = 1) //. or +
Return false;
If (length = 2 & start_pos = 1 & m_dot_count = 1) // +.
Return false;

Return true;
}

Bool isinterger ()
{
If (m_str.isempty ())
Return false;

Int start_pos = 0;
Char c = m_str.getat (0 );
If (c = '+' | c = '-')
Start_pos = 1;

Int length = m_str.getlength ();
For (int I = start_pos; I <length; I ++)
{
C = m_str.getat (I );
If (c <0x30 | c> 0x39)
Return false;
}

If (length = 1 & start_pos = 1) // +
Return false;

Return true;
}

Bool isvalidfloat (double minval, double maxval, bool ishasmin = true, bool ishasmax = true)
{
If (maxval <minval)
{
Double a = minval;
Minval = maxval;
Maxval =;
}

If (! Isfloat ())
Return false;

Double d = atof (lpctstr (m_str ));
If (ishasmin & d <minval)
Return false;
If (ishasmax & d> maxval)
Return false;

Return true;
}

Bool isvalidinterger (int minval, int maxval, bool ishasmin = true, bool ishasmax = true)
{
If (maxval <minval)
{
Int a = minval;
Minval = maxval;
Maxval =;
}

If (! Isinterger ())
Return false;

Int d = atoi (lpctstr (m_str ));
If (ishasmin & d <minval)
Return false;
If (ishasmax & d> maxval)
Return false;

Return true;
}
};

// Global functions
Class cstringvalid
{
Public:
// Valid String judgment
Static bool cstringvalid: strisfloat (const cstring & s)
{
Cstringex sx = s;
Return sx. isfloat ();
}

Static bool cstringvalid: strisnum (const cstring & s)
{
Cstringex sx = s;
Return sx. isinterger ();
}

Static bool cstringvalid: strisfloatvalid (const cstring & s, double minval, double maxval, bool ishasmin = true, bool ishasmax = true)
{
Cstringex sx = s;
Return sx. isvalidfloat (minval, maxval, ishasmin, ishasmax );
}

Static bool cstringvalid: strisintergervalid (const cstring & s, int minval, int maxval, bool ishasmin = true, bool ishasmax = true)
{
Cstringex sx = s;
Return sx. isvalidinterger (minval, maxval, ishasmin, ishasmax );
}
};


# Endif

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.