Jump to content

How to add validation in input field of tabular report form


Recommended Posts

Hi @Swagatika

You can try this solution instead:

 

<style>
.tooltip {
  position: relative;
}
.tooltip .tooltiptext {
  width: 120px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px;
  position: absolute;
  z-index: 1;
  top: 100%;
  left: 35%;
  text-wrap: wrap;
}

</style>

<script>
if (document.mainDataPageReadyHandler == undefined) { 
const inputName = 'Full_Name'


const addInputValidation = (InlineAddFormLightBox='') => {
let targetInput = document.querySelector(`${InlineAddFormLightBox} #InlineAdd${inputName}`)



if (targetInput==null) {return}
targetInput.addEventListener('input', e=>{
    
  if (/^([a-zA-Z\s])/.test(e.data) == false) {
    e.target.value = e.target.value.replace(e.data, '')
    addErrorToolTip(e.target)
    }
})
}


const addCopyPasteValidation = (InlineAddFormLightBox='') => {
let targetInput = document.querySelector(`${InlineAddFormLightBox} #InlineAdd${inputName}`)

if (targetInput==null) {return}

targetInput.addEventListener("paste", (e) => {
   e.stopPropagation()
   e.preventDefault()
   
   let clipboardData = e.clipboardData || window.clipboardData
   let pastedData = clipboardData.getData('Text')
   let pastedDataArr = pastedData.split('')
   for (let i=0;i<pastedDataArr.length;i++) {
    if (/^([a-zA-Z\s])/.test(pastedDataArr[i]) == false) {
    addErrorToolTip(e.target)
    return
   }
   }
   e.target.value += pastedData   
})
}

const addErrorToolTip = (input) => {
if (input.parentElement.classList.contains('tooltip')) {return}
input.parentElement.classList.add('tooltip')
input.insertAdjacentHTML('afterend', ` <span class="tooltiptext">Only characters allowed</span>`)
window.setTimeout(()=>{
input.parentElement.classList.remove('tooltip')
input.nextElementSibling.remove()
},3000)
}


const addMutationObserver = () => {
new MutationObserver((mutations)=>{
console.log(mutations)
for (let i=0; i<mutations.length;i++) {
if(mutations[i].addedNodes!=null) {
 if(mutations[i].addedNodes.length> 0) { 
 if (mutations[i].target.getAttribute('id')!=null) {
   if (mutations[i].target.getAttribute('id')=='InlineAddFormLightBox') {

   addInputValidation('#InlineAddFormLightBox')
   addCopyPasteValidation('#InlineAddFormLightBox')
   break
    
 }
 }
}
}
}

}).observe(document.querySelector('body'), {childList: true, subtree: true})

}



const mainDataPageReadyHandler = () => {
addInputValidation()
addCopyPasteValidation()
addMutationObserver()
}

document.addEventListener('DataPageReady', mainDataPageReadyHandler)
document.mainDataPageReadyHandler = 'enabled'
}
</script>


It will prevent input and show a tool tip with an error message if anything but a character or space is added to Full_Name inline add. It also accounts for copy-paste and inline add option on mobile version scenarios.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...