Thursday, March 28, 2024

Form button for two forms

Recently need line up two form submit buttons for two different forms. So I asked Copilot and Gemini. also the perplexity.

Here are the results:

Copilot:

<form>
    <!-- Form 1 content -->
    <button type="submit" formaction="/action_one">Submit Form 1</button>
    <!-- Form 2 content -->
    <button type="submit" formaction="/action_two">Submit Form 2</button>
</form>
Gemini:
<form action="form1_action.php" method="post">
  <input type="submit" value="Submit Form 1">
</form>

<form action="form2_action.php" method="post">
  <input type="submit" value="Submit Form 2">
</form>
Perplexity: - no two forms
<form action="/DEFAULT_URL" method="post">
  <!-- Input fields here -->
  <button type="submit">BUTTON 1</button>
  <button type="submit" formaction="/URL2">BUTTON 2</button>
</form>
So look not what I want, or I did not prompt properly? Any way, what I want is:
<form ...>
    ...
    ...
    <button ...>Save</button>
    <button form="back-form"...>Back</button>
</form>
<form id="back-form" ...></form>
sighing...

Monday, March 18, 2024

Regular Expressions 101

 /^.*ezsend\/link\/([\w-]+)\/([\w-]+)\/([\w-]+)?.*$

Regular expression sounds very intimating....Good site for testing - https://regex101.com/

Let's break down the regular expression ^.*ezsend\/link\/([\w-]+)\/([\w-]+)\/([\w-]+)?.*$:

^: Asserts the start of the string.

.*: Matches any character (.) zero or more times (*). This part is used to match any characters before the "ezsend/link" part of the URL.

ezsend\/link\/: Matches the literal string "ezsend/link/".

([\w-]+): This is the first capturing group, denoted by the parentheses. It matches one or more word characters (\w) or hyphens (-). Word characters include letters, digits, and underscores.

\/: Matches the forward slash / after the first token.

([\w-]+): This is the second capturing group, which matches the second token similar to the first one.

\/: Matches the forward slash / after the second token.

([\w-]+)?: This is the third capturing group, matching the third token similar to the first two. The ? makes this group optional, allowing the pattern to match URLs with or without a third token.

.*: Matches any remaining characters after the third token, if present.

$: Asserts the end of the string.

Here's how this regex works:

It starts by matching any characters before "ezsend/link/", ensuring that the URL contains this path.

Then, it matches the three tokens separated by forward slashes, allowing hyphens in each token.

The third token is optional, indicated by the ?.

Finally, it matches any remaining characters in the URL until the end of the string.

This regular expression is designed to capture the three tokens from URLs containing hyphens in each token, such as "xSwAAAAAAAA", "l-f1bdGQ92Q", and "ftG-CggAAAA".


ModelState.Clear vs TryValidateModel(manageModel)

 


`ModelState.Clear()` and `TryValidateModel(manageModel)` are both methods commonly used in ASP.NET Core applications to manage model validation.

1. `ModelState.Clear()`: This method is used to clear any model state errors that have been added to the `ModelState` dictionary. In ASP.NET Core, `ModelState` is a dictionary-like object that holds information about the state of model binding and validation for a given request. It stores information about model properties and their validation errors. Calling `ModelState.Clear()` removes all existing validation errors, effectively resetting the model state to a clean state.

2. `TryValidateModel(manageModel)`: This method is used to manually trigger model validation for a specified model object (`manageModel` in this case). By calling `TryValidateModel(manageModel)`, you are instructing ASP.NET Core to validate the specified model object against any validation rules defined in its associated data annotations or custom validation logic. If validation succeeds, the method returns `true`, indicating that the model is valid. If validation fails, the method returns `false`, and any validation errors are added to the `ModelState` dictionary.

In summary, `ModelState.Clear()` is used to clear any existing model state errors, while `TryValidateModel(manageModel)` is used to manually trigger model validation for a specific model object. These methods are often used together to ensure that model validation is performed correctly in ASP.NET Core applications.

evt.preventDefault vs evt.stopPropagation

 


evt.preventDefault()` and `evt.stopPropagation()` are both methods commonly used in JavaScript to control event propagation and behavior.

1. `evt.preventDefault()`: This method is used to prevent the default behavior of an event. In many cases, when an event (such as a click or form submission) occurs, the browser executes some default behavior associated with that event. For example, clicking on a link navigates the browser to the URL specified in the link's `href` attribute. By calling `evt.preventDefault()` inside an event handler function, you can prevent this default behavior from occurring. This is useful when you want to handle an event in a custom way, such as submitting a form via Ajax instead of the default form submission behavior.

2. `evt.stopPropagation()`: This method is used to stop the propagation of an event through the DOM tree. When an event occurs on an element, it typically "bubbles" up through the DOM tree, triggering event handlers on ancestor elements. By calling `evt.stopPropagation()`, you prevent the event from propagating any further in the DOM tree. This means that event handlers on parent elements will not be executed. This is useful when you want to handle an event on a specific element without triggering event handlers on its ancestors.

In summary, `evt.preventDefault()` is used to prevent the default behavior of an event, while `evt.stopPropagation()` is used to stop the event from propagating further through the DOM tree. Both methods are commonly used together to control event behavior in JavaScript applications.