Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize input Verification judgment by JavaScript

Shulou Source: shulou.com Published: 2022-06-01 10:31:10 11月01日 Update

This article mainly explains the "JavaScript how to achieve input verification judgment", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "JavaScript how to achieve input verification judgment" bar!

The try statement enables you to test errors in a block of code.

The catch statement allows you to handle errors.

The throw statement allows you to create custom errors.

Finally enables you to execute code, after try and catch, regardless of the result.

The try statement allows you to define a block of code to detect errors at execution time.

The catch statement allows you to define a block of code to execute if an error occurs in the block of try code.

The JavaScript statements try and catch appear in pairs:

Try {

Blocks of code for testing

}

Catch (err) {

A block of code that handles errors

}

When an error occurs, JavaScript usually stops and generates an error message.

The technical term goes like this: JavaScript will throw an exception (throw an error).

JavaScript actually creates an Error object with two properties: name and message.

Throw statement

The throw statement allows you to create custom errors.

Technically you can throw an exception (throw an error).

The exception can be a JavaScript string, number, Boolean, or object:

Throw "Too big"; / / throw text

Throw 500; / / throw a number

If you use throw with try and catch, you can control the program flow and generate custom error messages.

Enter a validation case

This example checks the input. If the value is incorrect, an exception (err) is thrown.

The err is caught by the catch statement and displays a custom error message:

Please enter a number between 5 and 10:

Tags: Error input statement code validation number message learning test content object technology processing control program between two creation regular meeting character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple Shulou Information NVidia Docker