DEV Community

Discussion on: Password Creation in Svelte

Collapse
 
artxe2 profile image
Yeom suyun

I think I would write the check function like this.

<script>
let pass = ""
let is_valid_length
let is_valid_case
let is_valid_special

const invalid_word_regex = /[\s<>]/g
const check_case_regex = /(?=.*[a-z])(?=.*[A-Z])/
const check_special_regex = /\W/

$: {
  pass = pass.replace(invalid_word_regex, "")
  is_valid_length = pass.length >= 8
  is_valid_case = check_case_regex.test(pass)
  is_valid_special = check_special_regex.test(pass)
}
</script>
<style>
  .valid {
    color: green
  }
</style>

<input bind:value={pass}>

<span class:valid={is_valid_length}>+8</span>
<span class:valid={is_valid_case}>a..Z</span>
<span class:valid={is_valid_special}>~&#</span>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
barim profile image
CodeCadim by Brahim Hamdouni

Totally valid way of using regex :-)