0

I have used a regex function on a textbox, but it's not working.

<input name="title" class="txtbxinput" id="title"
onblur="showsongstatus(this.value);title(this);" 
onkeyup="title(this);" size="58" >

function title(f) {
   f.value = f.value.replace(/[^a-zA-Z\s.'-]/gixsm,'');
   f.value = f.value.replace(/\s\s+/g, '\s'); //remove more than 2 white spaces.
   f.value = f.value.replace(/'{2,}/g, '\'');
   f.value = f.value.replace(/--/g, '-');
   f.value = f.value.replace(/\.\./g, '\.');
return f;
}

I have restricted it for alphabets, hyphen, dots, and single quotes.

2 Answers 2

1

Your problem is that you are using a function named title that happens to have the same name as your id.

To fix, just change your function name. For example, the following works:

<input name="title" class="txtbxinput" id="title"
onblur="showsongstatus(this.value); doTitle(this);" 
onkeyup="doTitle(this);" size="58" >

function doTitle(f) {
   f.value = f.value.replace(/[^a-zA-Z\s.'-]/gixsm,'');
   f.value = f.value.replace(/\s\s+/g, '\s'); //remove more than 2 white spaces.
   f.value = f.value.replace(/'{2,}/g, '\'');
   f.value = f.value.replace(/--/g, '-');
   f.value = f.value.replace(/\.\./g, '\.');
return f;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can write javascript code like:

<input type="text" id="txt" onkeypress="return check(event);" />

function check(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode            
            if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) || charCode == 39 || charCode == 46 || charCode == 45)
                return false;
            else
                return true;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.