Reformat User-Entered Data Before You Validate

Reformat User-Entered Data Before You Validate

If you expect a user to enter a numerical value in a text box, you’ll want to do some data validation on the number. But suppose he’s included a dollar sign in the front of an otherwise legitimate dollar amount, or interspersed parentheses, spaces, or dashes in among the digits of his phone number? One solution is to reformat the entered string, removing the characters that might have been inadvertently included, and then go on to test the string for a legitimate numerical value. There are several reformatting methods you could use; one generic approach is to use a function like stripChars, below:

 function stripChars(str, chars) {var ivar newstring = ""for (i = 0; i < str.length; i++) {mychar = str.charAt(i)if (chars.indexOf(mychar) == -1)newstring += mychar}return newstring}

The stripChars function accepts two parameters: the input string in 'str', and a string made up of all the specific characters you want removed in 'chars'. The function loops through each character in the input string. Using the String method indexOf, it checks to see if the character is contained within chars; if it's not, the character is added to a new string variable, 'newstring'. Recall that the String method indexOf returns a -1 if the substring isn't found in the referenced string. When the function is done, it returns the reformatted string.

For example, suppose the user had entered a phone number, (716) 555-1436, in a text box named 'phone'. If we called stripChars(phone.value, "() -" ) the function would return the string 7165551436.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as