disable special characters in custom code

main
TZGyn 1 year ago
parent c8282dfbe9
commit 45b5f655ce
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -87,3 +87,7 @@ Number.prototype.toDecimalPoint = function (decimal: number) {
return Math.round(this.valueOf() * value) / value return Math.round(this.valueOf() * value) / value
} }
export const isAlphanumeric = (str: string) => {
return str.match('^[A-Za-z0-9]+$')
}

@ -15,6 +15,7 @@ import { zod } from 'sveltekit-superforms/adapters'
import { formSchema } from './schema' import { formSchema } from './schema'
import type { Actions } from './$types' import type { Actions } from './$types'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { isAlphanumeric } from '$lib/utils'
export const load = (async (event) => { export const load = (async (event) => {
const user = event.locals.user const user = event.locals.user
@ -149,6 +150,14 @@ export const actions: Actions = {
'Please Enter Custom Code', 'Please Enter Custom Code',
) )
} }
if (!isAlphanumeric(form.data.custom_code)) {
return setError(
form,
'custom_code',
'Code cannot contain special characters',
)
}
const customCodeExist = await db.query.shortener.findFirst({ const customCodeExist = await db.query.shortener.findFirst({
where: (shortener, { eq }) => where: (shortener, { eq }) =>
eq(shortener.code, form.data.custom_code), eq(shortener.code, form.data.custom_code),

@ -7,6 +7,7 @@ import { db } from '$lib/db'
import { redirect } from '@sveltejs/kit' import { redirect } from '@sveltejs/kit'
import { shortener } from '$lib/db/schema' import { shortener } from '$lib/db/schema'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { isAlphanumeric } from '$lib/utils'
export const load = (async (event) => { export const load = (async (event) => {
const user = event.locals.user const user = event.locals.user
@ -79,6 +80,14 @@ export const actions: Actions = {
'Please Enter Custom Code', 'Please Enter Custom Code',
) )
} }
if (!isAlphanumeric(form.data.custom_code)) {
return setError(
form,
'custom_code',
'Code cannot contain special characters',
)
}
const customCodeExist = await db.query.shortener.findFirst({ const customCodeExist = await db.query.shortener.findFirst({
where: (shortener, { eq, and, ne }) => where: (shortener, { eq, and, ne }) =>
and( and(

@ -15,6 +15,7 @@ import { zod } from 'sveltekit-superforms/adapters'
import { formSchema } from './schema' import { formSchema } from './schema'
import type { Actions } from './$types' import type { Actions } from './$types'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { isAlphanumeric } from '$lib/utils'
export const load = (async (event) => { export const load = (async (event) => {
const { project: selectedProject } = await event.parent() const { project: selectedProject } = await event.parent()
@ -119,6 +120,14 @@ export const actions: Actions = {
'Please Enter Custom Code', 'Please Enter Custom Code',
) )
} }
if (!isAlphanumeric(form.data.custom_code)) {
return setError(
form,
'custom_code',
'Code cannot contain special characters',
)
}
const customCodeExist = await db.query.shortener.findFirst({ const customCodeExist = await db.query.shortener.findFirst({
where: (shortener, { eq }) => where: (shortener, { eq }) =>
eq(shortener.code, form.data.custom_code), eq(shortener.code, form.data.custom_code),

@ -7,6 +7,7 @@ import { db } from '$lib/db'
import { redirect } from '@sveltejs/kit' import { redirect } from '@sveltejs/kit'
import { shortener } from '$lib/db/schema' import { shortener } from '$lib/db/schema'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { isAlphanumeric } from '$lib/utils'
export const load = (async (event) => { export const load = (async (event) => {
const { project: selectedProject } = await event.parent() const { project: selectedProject } = await event.parent()
@ -63,6 +64,14 @@ export const actions: Actions = {
'Please Enter Custom Code', 'Please Enter Custom Code',
) )
} }
if (!isAlphanumeric(form.data.custom_code)) {
return setError(
form,
'custom_code',
'Code cannot contain special characters',
)
}
const customCodeExist = await db.query.shortener.findFirst({ const customCodeExist = await db.query.shortener.findFirst({
where: (shortener, { eq, and, ne }) => where: (shortener, { eq, and, ne }) =>
and( and(

Loading…
Cancel
Save