-1

I am writing this code to save or update employee data in an object. If the ID number field is empty, then it is a new employee and a random employee ID will be generated and if an ID is provided, then the ID will be searched and the record will be updated. But I need to validate that the ID input is a number before searching. The regular expression is allowing letters to pass through but I want it to alert "ID must be a number" and return false. The code is below:

//Javascript Code
const recordCollection = {
         
    }

    function userRecords() {
        let idNum = document.getElementById('idNum').value;
        let firstName = document.getElementById('firstName').value;
        let lastName = document.getElementById('lastName').value;
        let age = document.getElementById('age').value;
        let department = document.getElementById('department').value;
        let role = document.getElementById('jobRole').value;
        let level = document.getElementById('level').value;
        let demo = document.getElementById('demo');
        let regex = /^[0-9]+$/;
        

            // generate employer id
            if (idNum === "") { 
            //if there is no id, generate a four(4)-digit number id for the employer
                idNum = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
            } else if (!idNum.match(regex)) { 
            //check if id is a number
                alert("ID must be a Number");
                return false;
            } else if (idNum.length !== 4) { 
            //check if id is equal to 4
                alert("ID must be four(4) numbers");
                return false;
              } 
            }           
            //create or update a record
        // recordCollection["id"] = idNum;
        recordCollection["First Name"] = firstName;
        recordCollection["Last Name"] = lastName;
        recordCollection["age"] = age;
        recordCollection["department"] = department;
        recordCollection["Job Role"] = role;
        recordCollection["Level"] = level;

        demo.innerHTML = JSON.stringify(recordCollection, null, 2);
        return demo;
            <!-- HTML code-->
                <aside>
                    <div>
                        <div>
                            <h3>Add or Update User Information</h3>
                            <div>
                                <label>ID No.</label>
                                <input type="number" id="idNum">
                            </div>
                            <div>
                                <label>First Name</label>
                                <input type="text" id="firstName">
                            </div>
                            <div>
                                <label>Last Name</label>
                                <input type="text" id="lastName">
                            </div>
                            <div>
                                <label>Age</label>
                                <input type="number" id="age">
                            </div>
                            <div>
                                <label>Department</label>
                                <input type="text" id="department">
                            </div>
                            <div>
                                <label>Job Role</label>
                                <input type="text" id="jobRole">
                            </div>
                            <div>
                                <label>Level</label>
                                <input type="text" id="level">
                            </div>
                            <div>
                                <button onclick="userRecords();">Submit</button>
                            </div>
                        </div>
                    </div>
                </aside>

                <section>
        <h3>Result Below</h3>
                    <pre id="demo"></pre>
                </section>
    

2
  • 1
    What does “not working” mean exactly? Can you elaborate on what errors you’re seeing, or what you’re expecting versus what actually happens? How to Ask
    – esqew
    Commented Jul 22, 2022 at 17:26
  • @esqew Okay the code passes letters and numbers as well in the ID input field. But I want it to alert "ID must be a number" whenever a letter or letters are used in the ID
    – Skynet
    Commented Jul 22, 2022 at 17:30

1 Answer 1

2

String.match returns an Array.

I think what you are looking for is Regex.test(String)

const someIds = ['abcd', '12as', '1234', '123', '123a45', '12345'];
const regex = /^[0-9]+$/;

someIds.forEach(id => console.log("id is valid: ", regex.test(id)))

Also you could extend your regex to only accept four numbers:

/^[0-9]{4}$/
3
  • How do I use regex.test(string)?
    – Skynet
    Commented Jul 22, 2022 at 17:40
  • 1
    /^[0-9]{4}$/.test("thats-the-way-how-you-use-it") Commented Jul 22, 2022 at 17:47
  • Still not working for me. I realized in my original code that if I comment out all the other "if" statement and leave only the regular expression, it works. So could any of the "if" statements be interfering in it?
    – Skynet
    Commented Jul 22, 2022 at 17:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.