# code-snippets/3.2.1/js/edit/validate.ts

Code Snippets, version 3.2.1. 24 lines.

- Page: https://pluginprobe.com/plugins/code-snippets/3.2.1/code/js/edit/validate.ts
- Raw: https://pluginprobe.com/plugins/code-snippets/3.2.1/raw/js/edit/validate.ts
- Modified: 2022-10-05T07:00:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-snippets/3.2.1/code/js/edit/validate.ts#L10-L20`.

```typescript
export const handleFormSubmitValidation = () => {
	const form = document.getElementById('snippet-form')
	const editor = window.code_snippets_editor?.codemirror
	const strings = window.code_snippets_edit_i18n
	const snippetName = document.querySelector('input[name=snippet_name]') as HTMLInputElement

	if (!form || !editor || !snippetName) {
		return
	}

	form.addEventListener('submit', (event: SubmitEvent) => {
		const missingTitle = '' === snippetName.value.trim()
		const missingCode = '' === editor.getValue().trim()

		const message = missingTitle ?
			missingCode ? strings.missing_title_code : strings.missing_title :
			missingCode ? strings.missing_code : ''

		if (event?.submitter?.id.startsWith('save_snippet') && message && !confirm(message)) {
			event.preventDefault()
		}
	})
}

```
