Last active
October 8, 2022 12:45
-
-
Save Azhar2k16/b5fce456b435d958e933f27d2797b379 to your computer and use it in GitHub Desktop.
Want to Mask the phone number input to DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Schema[7.0].define(version: 2022_10_08_122055) do | |
create_table "employees", force: :cascade do |t| | |
t.string "f_name" | |
t.string "l_name" | |
t.string "email" | |
t.string "phone" | |
t.datetime "created_at", null: false | |
t.datetime "updated_at", null: false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="my-5" data-controller="phoneinput"> | |
<%= form.label :Phone %> | |
<%= form.text_field :phone, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full", data: { 'phoneinput-target': 'input'}, :maxlength=>"12", placeholder: "555-111-5555" %> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from "@hotwired/stimulus" | |
export default class extends Controller { | |
static targets = [ "input" ] | |
connect() { | |
const input = this.inputTarget; | |
// console.log("hellow"); | |
input.addEventListener("keydown", (e) => { | |
if (e.key === "Backspace" || e.key === "Delete") return | |
if (e.target.value.length === 3) { | |
input.value = input.value + "-" | |
} | |
if (e.target.value.length === 7) { | |
input.value = input.value + "-" | |
} | |
if(e.target.value.length === 14) { | |
input.value = input.value + "-"; | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment