Question

In: Computer Science

So I'm writing a function in javaScript that will take a user full name in one...

So I'm writing a function in javaScript that will take a user full name in one text box. "first" space "last name" But if the user does not enter the space then there should be an error. So I'm looking for a way to validate this so the user needs to enter the space.

Solutions

Expert Solution

Since we have to validate a pattern, we can use Regular Expressions. We can assign a pattern in a variable and compare it with the user's input to validate.

The following code explained:

1. Store the regular expression in a variable "regularExpression"

The regular expression is: ^[a-zA-Z]+ [a-zA-Z]+$

Note that the first [a-zA-Z] is for the first name and the second is for last name.

a-z : It is for lowercase letters

A-Z : It is for uppercase letters

Note: There is a space after the + operator which denotes a space between the two names.

2. The user's input will be stored in variable "name"

3. In the if statement, use the test( ) method to match the input string and the regular expression. If the condition doesn't satisfy , print an alert message and return false.

4. In the else block, which means that the pattern has matched and there is a space between both the names.So, alert the message as "Entered name is valid" and return true.

Code:

function validate(){

    var regularExpression = /^[a-zA-Z]+ [a-zA-Z]+$/;

    var name = document.getElementById('name').value;

    if(!regularExpression.test(name)){

        alert('Error! Enter your full name - First Name and Last Name');
        document.getElementById('name').focus();
        return false;
    }
    else{
        alert('Entered name is valid');
        return true;
    }
}

This is the entire code:

<html>

<head>
<title>Example</title>
</head>

<body>
<p>Full Name: <input id="name" value=""></p>
<input type="button" onclick="validate();" value="Validate Name">
<script>
function validate(){
    var regularExpression = /^[a-zA-Z]+ [a-zA-Z]+$/;
    var name = document.getElementById('name').value;
    if(!regularExpression.test(name)){
        alert('Please enter your full name (first & last name).');
        document.getElementById('name').focus();
        return false;
    }else{
        alert('Valid name given.');
        return true;
    }
}
</script>

</body>
</script>

Related Solutions

JAVASCRIPT HTML I'm looking to make a function that will take a postal code in a...
JAVASCRIPT HTML I'm looking to make a function that will take a postal code in a text box. The function jobs is to basically make sure the first letter of the postal code starts with these letters. ('A') ('N") ('W') ('F'). If the first letter of the postal code does not match up then an error to the user is sent.
write a javascript function called cal_modes that will take a List of numbers or strings as...
write a javascript function called cal_modes that will take a List of numbers or strings as input and returns a List of the most frequent values. If there's only one most-frequent value, it returns a single-element List. example cal_modes([3,4,5,5]) should return [5] cal_modes([8.9, 1, 1]) should return [1] cal_modes([2.5, -2, -2, 2.5]) should return [2.5] cal_modes([3,3,4,4]) should return [3,4] cal_modes([3,4,5]) should return [3,4,5], because all occur with equal frequency cal_modes(["hi", "what", "where", "hi"]) should return ["hi”]
In.java Write down a program that asks the user for their full name given in the...
In.java Write down a program that asks the user for their full name given in the format first last. The program will do the necessary processing and print the name in a table with 3 columns: Last, First, Initials. Requirements/Specifications... Your program run must be identical with the one shown as an example (both in how it reads the input and it on what it prints). The table must have the top and bottom lines as shown. The columns for...
Write a program that loops, prompting the user for their full name, their exam result (an...
Write a program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record each...
C++ Change the program to take user input for first name and last name for five...
C++ Change the program to take user input for first name and last name for five employees. Add a loop to read the first name and last name. // EmployeeStatic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> #include <string> using namespace std; class Employee { public:    Employee(const std::string&, const std::string&); // constructor    ~Employee(); // destructor    std::string getFirstName() const; // return first name    std::string getLastName() const; // return...
Put your name on the first line in camelCase. Write a JavaScript function to calculate the...
Put your name on the first line in camelCase. Write a JavaScript function to calculate the revenue for blocks sales. Input the number of wooden blocks, the number of plastic blocks and the number of rubber blocks. Wooden blocks are $5. Plastic blocks are $2 and rubber blocks are $4. Calculate the cost for each type of block. Calculate the total cost. Display the four outputs on the page. Label the inputs and the output on your page. Change the...
Python Write a program that loops, prompting the user for their full name, their exam result...
Python Write a program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
Write a python program that loops, prompting the user for their full name, their exam result...
Write a python program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
Write a javascript code to Create a function called Hotel that takes Room no, Customer name....
Write a javascript code to Create a function called Hotel that takes Room no, Customer name. amount paid. Write a code to call hotel function for each customer and display details of customers lodging in rooms with even room numbers. I need only js and html code. no css pls take screenshot of output , else I might dislike thanks
Write a brief shell script that will take in a specific file name, prompt the user...
Write a brief shell script that will take in a specific file name, prompt the user whether they would like to gzip, bzip2, or xz compress the file. Depending on response, the script then ought to compress the provided file with the corresponding method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT