Character Limit for Text Box for a telephone number - c#

I'm doing a project in which I take in a customers details (Title, Forename, Contact Number etc) and store them in a database. (The database and UI are in the same project!) I have a sign up form in which I've created a click event where it should sign a customer up to the system. For validation, I was wondering how I would go about setting the length for a telephone number. my telephone number data type is set as string in the UI(User Interface) and the user has to use a text box to enter their telephone number.
I would like the length to be set as 11 but can't seem to set it as this without the system throwing errors and exceptions. Here's what I've done so far:
if (string.IsNullOrEmpty(cmbxTitle.Text) ||
txtForename.Text = "" ||
txtSurname.Text = "" ||
txtContactNo.Text = "" ||
txtContactNo < //this is where I need help)
{
MessageBox.Show("");
}
My ContactNo variable is the variable I wish to set to be exactly 11 characters. Any suggestions?
Using c# and on Visual Studio 2022

Related

c# - Splitting a string with Email Id in it is truncating the last 3 characters of the Email

I have a readonly textbox field which a value in it like 'Dan, Sealey - Dan.s42#KPS.com' , I want to retrieve the email id from the above string.
I tried using the below code
string email = Convert.ToString(this.txtUser).split('-')[1].Trim();
I even tried in the below way as well but no use, still the result is same.
string str = Convert.ToString(this.txtUser);
string trimmedString = str.Substring(str.IndexOf("-") + 1);
The issue i am having with the above line of code is it is truncating .com and giving the value as "Dan.s42#KPS..."
Not able to figure out why this is truncating ".com"
Any help on this ?
Looks like the issue is with this.txtUser text box. Debug what value is it returning. Check the max length or any limitation on length property. See if there's any masked control trimming the text based on length limitation.
issue resolved, instead of this.txtUser i had to use this.txtUser.text

Set up culture for input text box when writing number

I just using a kendo drop down list for my project
var editorElement = (RadDropDownListEditorElement)editor.EditorElement;
editorElement.KeyPress += EditorElement_KeyPress;
editorElement.FormatString = "D9";
This can use to input in both way, select data from drop down list & write directly from keyboard,I manage to localize number with Value Member & Display Member when choosing from list. But I'm stuck at this when use input from keyboard, whenever I input number it's always have default display like this
15769.9292
It only convert to current culture in my project when I switch to another window
15 769,9292 //fr-FR
15,769.9292 //en-US
Is there anyway that we can set up culture format even when we input number, like display comma instead of dot when we write number in French?

Text box translation into address

I work in an environment where we do a lot of remote troubleshooting. I am developing a C# Console Windows Application that speeds the process of logging into different programs.
It is a multi-tabbed application where on the first Window, there is a text box where the user enters a site number.
What I would like help with is how can I write a code that will read that 3 or 4-digit site number and translate it into an IP address?
In one part of the application, I need it to translate it into an IP address inside a URL. For example, say the user wants to connect to a server at site 1234. They enter 1234 in the text box on the main page and then click connect on the Server tab. I need the site number to implant itself inside a URL looking something like //10.12.34.1:80/.
I'll answer this only for the sake of showing you that your simplified requirements can be easily achieved.
If you only need to decide between 3 and 4 char input values and hardcode the start and end of the target IP address this should do the trick:
string userinput; // 3 or 4 char user input from TextBox
string secondoctet = string.Empty;
string thirdoctet = string.Empty;
if (userinput.Length == 3)
{
secondoctet = userinput.Substring(0, 1);
thirdoctet = userinput.Substring(1, 2);
}
if (userinput.Length == 4)
{
secondoctet = userinput.Substring(0, 2);
thirdoctet = userinput.Substring(2, 2);
}
// using hardcoded IP format template inserting 2nd and 3rd octet into placeholders
string targetip = string.Format("10.{0}.{1}.1", secondoctet, thirdoctet);
Please keep in mind that this solution is restricted to the simple rule that 3-char input will always be split in 1-2 and 4-char input will always be split in 2-2.
As already mentioned in my comment without doubt you will soon face requirements for splitting according to different rules.

Selecting a part of a string according to a variable

First of all I'm sorry about the title, I didn't know how to formulate what I want to do.
Here is a part of the string I want to analyze :
Microsoft Application Error Reporting<br><br />Microsoft Application Error Reporting<br><br />Microsoft Office Professional Plus 2010<br><br />Microsoft Office OneNote MUI (English) 2010<br><br />
And here is the website where I display the string :
Website
Now what I'd like to do : When I click a checkbox, I'd like to take the part of the string associated to the checkbox' line and put it in another string.
For example, if I select the 9th checkbox, I'd like to put Microsoft Office Excel MUI (English) 2007 in a new string.
I've made a piece of code going to the specified line, but I don't know how to use it now...
int i=0;
do
{
int j = 0;
while ((j = (InstalledSoftwares.Text).IndexOf("<br><br />", j)) != -1)
{
j += "<br><br />".Length;
i++;
}
} while (i <= Convert.ToInt16(TextBoxTest.Text));
InstalledSoftwares.Text is the string I want to analyze.
TextBoxTest contains the number of the checkbox.
I searched a lot to find an answer to my problem but didn't find anything.. Thanks a lot for your help, and sorry for my english.
You should be using the Split option to split the text and then pick the relevent text from the array with relation to the checkbox checked
InstalledSoftwares.Text.Split(new []{"<br>< br/>"},
StringSplitOptions.RemoveEmptyEntries);
Now you can use the index 8 to get the relevant text for checkbox 9 (since array is zero indexed)
Hope this helps.
Using Regex
Split Pattern = #"\s*<br>\s*<br\s*/>\s*"
Product Name = CheckBox Index - 1
String[] Products = Regex.Split(XML, Pattern);
String Product = Products[ CheckBoxIndex - 1 ];

Extracting data from text using templates

I'm building a web service which receives emails from a number of CRM-systems. Emails typically contain a text status e.g. "Received" or "Completed" as well as a free text comment.
The formats of the incoming email are different, e.g. some systems call the status "Status: ZZZZZ" and some "Action: ZZZZZ". The free text sometimes appear before the status and somethings after. Status codes will be mapped to my systems interpretation and the comment is required too.
Moreover, I'd expect that the the formats change over time so a solution that is configurable, possibly by customers providing their own templates thru a web interface would be ideal.
The service is built using .NET C# MVC 3 but I'd be interested in general strategies as well as any specific libraries/tools/approaches.
I've never quite got my head around RegExp. I'll make a new effort in case it is indeed the way to go. :)
I would go with regex:
First example, if you had only Status: ZZZZZ- like messages:
String status = Regex.Match(#"(?<=Status: ).*");
// Explanation of "(?<=Status: ).*" :
// (?<= Start of the positive look-behind group: it means that the
// following text is required but won't appear in the returned string
// Status: The text defining the email string format
// ) End of the positive look-behind group
// .* Matches any character
Second example if you had only Status: ZZZZZ and Action: ZZZZZ - like messages:
String status = Regex.Match(#"(?<=(Status|Action): ).*");
// We added (Status|Action) that allows the positive look-behind text to be
// either 'Status: ', or 'Action: '
Now if you want to give the possibility to the user to provide its own format, you could come up with something like:
String userEntry = GetUserEntry(); // Get the text submitted by the user
String userFormatText = Regex.Escape(userEntry);
String status = Regex.Match(#"(?<=" + userFormatText + ").*");
That would allow the user to submit its format, like Status:, or Action:, or This is my friggin format, now please read the status -->...
The Regex.Escape(userEntry) part is important to ensure that the user doesn't break your regex by submitting special character like \, ?, *...
To know if the user submits the status value before or after the format text, you have several solutions:
You could ask the user where his status value is, and then build you regex accordingly:
if (statusValueIsAfter) {
// Example: "Status: Closed"
regexPattern = #"(?<=Status: ).*";
} else {
// Example: "Closed:Status"
regexPattern = #".*(?=:Status)"; // We use here a positive look-AHEAD
}
Or you could be smarter and introduce a system of tags for the user entry. For instance, the user submits Status: <value> or <value>=The status and you build the regex by replacing the tags string.

Categories