Script for validating forms that utilizes regular expressions to search for multiple keywords spanning line breaks

I am trying to implement a validation process in which students must include specific keywords in their lesson summaries before submission. The current regex I have works well, except when the student creates a new paragraph using the enter key.

/^(?=.*\bmodel|models\b)(?=.*\btheory|theories\b)(?=.*\blaw|laws\b)(?=.*\bscale\b)/i;

I have tried modifying the regex to allow for reading across line breaks, but so far I have not been successful. Any guidance on what I might be doing wrong would be greatly appreciated as I am still learning in this area. Many thanks!

var ck_summary = /^(?=.[\S\s]*\bmodel|models\b)(?=.[\S\s]*\btheory|theories\b)(?=.[\S\s]*\blaw|laws\b)(?=.[\S\s]*\bscale\b)/i;

As a beginner in this field, I am eager to understand and improve my coding skills. Below is the script I am using for summary validation...

var ck_studentid = /^[0-9]{6,6}$/;
var ck_summary = /^(?=.[\S\s]*\bmodel|models\b)(?=.[\S\s]*\btheory|theories\b)(?=.[\S\s]*\blaw|laws\b)(?=.[\S\s]*\bscale\b)/i;

function validate(sumform){
     var studentid = sumform.studentid.value;
     var summary = sumform.summary.value;

     var errors = [];

     if (!ck_studentid.test(studentid)) {
          errors[errors.length] = "Check your Student ID";
     }

     if (!ck_summary.test(summary)) {
          errors[errors.length] = "Make sure you used all of the vocabulary terms AND you spelled them correctly.";
     }

     if (errors.length > 0) {
          reportErrors(errors);
          return false;
     }
     return true;
}

function reportErrors(errors){
     var msg = "Uh oh, looks like we have a problem...\n";
     for (var i = 0; i<errors.length; i++) {
          var numError = i + 1;
          msg += "\n" + numError + ". " + errors[i];
     }
     alert(msg);
}

Answer №1

After doing some research, it appears that by making the following adjustment to the regex, my validation script should be able to process line breaks. However, I have not been successful in implementing this change.

 var ck_summary = /^(?=.[\S\s]*\bmodel|models\b)(?=.[\S\s]*\btheory|theories\b)(?=.[\S\s]*\blaw|laws\b)(?=.[\S\s]*\bscale\b)/i;

Your previous attempt failed because the modification was not thorough enough.

.*: captures every character or word except a newline \n, which is why you were advised to adjust it to [\S\s]* to include newlines as well. However, in your pattern, there are still dots . before [\S\s]*. You need to remove all the dots . before [\S\s]*.

The corrected code should look like this

var ck_summary = /^(?=[\S\s]*\bmodel|models\b)(?=[\S\s]*\btheory|theories\b)(?=[\S\s]*\blaw|laws\b)(?=[\S\s]*\bscale\b)/i;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Streamline uploading files with AngularJS using Selenium

I am utilizing Powershell to operate .NET Selenium with a FirefoxDriver in order to automate certain tasks. One of these tasks involves file uploads, and the website I am working with appears to have been built using AngularJS. After some experimentation, ...

What causes the image to not appear at the center bottom of the page when using IE for loading?

Why does the loading image not appear at the center bottom of the page in IE? This function loads content when the page is loaded and also loads content when scrolled to the bottom. When you load the page index.php, you will see the loading image at the ...

How come the default is operating when the number is specifically set to 1?

let spans = document.querySelector(`#spans`); let hrs = document.querySelector(`#hrs`); let mins = document.querySelector(`#mins`); let secs = document.querySelector(`#secs`); let start = document.querySelector(`#start`); let stop = document.querySelector( ...

Experiment with parsing multiple outputs instead of repeatedly coding them as constants in Node.js

In my application, I am parsing multiple database data using cron and currently, I have it set up to create a const run for each data to find the dag_id and manually test the determineCron function one at a time. How can I create an array so that the "dete ...

Maintain the initial value using ko.mapping.fromJS

How can I prevent new data fetched from an AJAX call using ko.mapping.fromJS from replacing the existing data in my observableArray? I want to append the new data without losing the previous entries. function ViewModel() { var self = this; self. ...

why is the sum coming out as an undefined number?

My challenge involves creating a table that should display the total price, however, it keeps showing NaN. The code snippet below outlines how the total price is calculated: import React from 'react'; const Total = (props) => { const {ite ...

Encountering a challenge when attempting to upgrade to Angular 9: Issue with transitioning undecorated classes with DI migration

As I transition from Angular 8 to Angular 9, I encounter an error during the step related to transitioning undecorated classes with DI. While attempting to migrate, I faced an issue with undecorated classes that utilize dependency injection. Some project ...

When going through a list of children, it is important that each one has a distinct "key" property. This is essential for properly iterating over divs

I am encountering the following warning message: "Each child in a list should have a unique "key" prop. Check the render method of Todo. in TodoItem (at ToDo.js:14)" Here is the code snippet: import React from 'react'; import ...

What is the best way to include two class names within a single div using Next.js?

Struggling to include two different CSS classes into a single div element, I encountered some issues. For reference, here is a screenshot: https://i.stack.imgur.com/UuCBV.png https://i.stack.imgur.com/sHNwq.png My code snippet looks like this: blog.js ...

perform a JSON request in a RESTful manner

Can you explain the concept of making a RESTful JSON request? In the given scenario, when Backbone attempts to read or save a model to the server, it calls upon the function known as Backbone.sync. This function, by default, utilizes (jQuery/Zepto).ajax t ...

Using NODE to create engaging user interfaces for command line interactions

When the command npm init is executed, it prompts a series of questions and records the answers in a file. I am interested in developing a similar command line utility using node. Are there any existing packages that could help with this project? If poss ...

Storing a blank field in a Kendo grid

Is there a way to clear the content of a cell and save it as an empty field? I attempted to specify my field in the parameter update map using the following code: ((data.Value) ? '","Value": "' + data.Value : "") + and in the schema like this: ...

Encountering difficulties with rendering SVG within a React application

Struggling to incorporate an SVG file from the same folder as my js file into my React application, but all I see is the default no image. Using version 1.6.9 of React. I've attempted dragging and dropping the contents of the SVG file, as well as sea ...

Ways to send a command line parameter to an embedded script?

IMPORTANT: This concerns passing arguments not to the main script, but to a script called by that script When I specify command line arguments directly in my package.json script, it works fine. However, when I call a script that then calls another script, ...

Angular7 is throwing an error saying it cannot read the property 'post' of undefined

Looking to create a custom validation in Angular 7. Here is the code snippet: CheckUserNameExisit(control: AbstractControl): { [key: string]: any } { console.log('in functions') return new Promise(resolve => { let httpClient: HttpClient ...

Node.js equivalent of hash_hmac

I am facing an issue while trying to sign the URL in a Node.js application similar to how it is done in my PHP app. In PHP, I use the following code to sign the URL: private static function __getHash($string) { return hash_hmac('sha1', $stri ...

How do I prevent the React <div id="root"></div> from disrupting the layout of my design?

I am working on implementing a ReactJS template, but the footer keeps breaking and I need it to stay at the bottom. The main culprit is the <div id="root"></div>. Does anyone know how to fix this issue? https://i.sstatic.net/xNRKe.png import ...

There seems to be an issue with my view loading CSS/JS files that are not located within

I have been working on my MVC 3 application and encountered some challenges with loading CSS, images, and JS files from the root folder. When I tried to run my view located in the Views folder, I faced the following output: <link href="@Url.Content("~ ...

Utilizing ion-slide-box within an ion-content container that allows for scrolling

I've created an Ionic view with the following structure: <ion-content scroll="true"> <ion-list> ... some ion items... <ion-item> <ion-slide-box> <ion-slide ng-repeat="image i ...

Filtering by labels

I need to search for the label "1125" and if it is not found, I want to show an alert box. Here is my code: if (var id = $('mobilelabel_2:contains("1125")').attr('for');) { alert ("works"); } else { alert ("try again"); } ...