Validating field length

Class library usage, coding and language questions.
regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Validating field length

Post by regex » Thu Sep 20, 2012 3:48 pm

Validate length of text in field.

I am using the following Regex which doesn't compile and gives general error:

Code: Select all

   public void charLimit()
        {
            Report.Log(ReportLevel.Info, "Get Value", " 'charLimit'.", SubmitFormTextBoxInfo);
            charLimit = Element.GetAttributeValueText("InnerText");
            
            try
            {
            	if(charLimit.Contains(^([2)$)) 
        	
        			Report.Failure("The field limits characters to 200 and 2 was found which requires a fail.  The digit is the 101 character"); 
        		}
            
            else 
            {
            	Report.Success("the test passes"); 
            }
            
        	
        	catch
            {
            	Report.Failure("the two elements do not match"); 
            }
            }
        }

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Validating field length

Post by Support Team » Fri Sep 21, 2012 9:05 am

Hi,

what exactly are your trying to perform?
If you are going to validate if the value of your text box is smaller than 200 then simply validate this:
if(Convert.ToInt32(charLimit) >= 200)
If you are going to validate, using a regular expression, don't use the method contains but following code snippet:
Validate.Attribute(Element, "InnerText", new Regex("<Your Regular Expression>"));
Regards,
Tobias
Ranorex Team
.
Image

regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Re: Validating field length

Post by regex » Fri Sep 21, 2012 9:59 pm

Thank you. This worked. I just have a problem with null values. I did put in:

Code: Select all

if(limit == null)
{
 limit = "null"; 
}

Code: Select all

public void VerifyLimitFax()
        {
             	
        		//check the repository item
        		string limitCheck = TextField.Element.GetAttributeValueText("InnerText");;
            	string intK = string.Empty; 
            	int k = limitCheck.Length; 
            	//limit is set here...
            	int limit = 26;
            	
            	Report.Log(ReportLevel.Info, "Get Value", "Getting attribute 'InnerText' from item 'TextField' and assigning its value to variable 'phoneInnerText'.", repo.portal.QA.PhoneTextFieldInfo);

            	intK = k.ToString(); 
            	
            	if(limitCheck == null)
            	{
            		limitCheck = "null";  
            		
            	}
            	
            	if(k >= limit)
            	
	            {
	            	Report.Failure("Fail.  Limit does not seem to match"); 
	            }
            		
	            else
	            {
	            	Report.Success("Pass.  Limit seems to match the requirements"); 
	            }
            	
            	Report.Info(intK);
            	Report.Info(limitCheck);
            	
            	
            	
            	}