Skip to content Skip to sidebar Skip to footer

Validating A Text Box Value Using Javascript

I have a textbox.The user will be entering entering a number in this box.The user should enter two digits followed by a period '.' and another digit.It would look something like 22

Solution 1:

Excuse the Prototype specific code, but maybe you'll get the gist of it. You'll want to listen on the "onblur" event of the textarea, and then validate the entered value with a regular expression.

Event.observe('textboxId', blur, function() {
  var val = $F('textboxId');
  if (/^\d{2}\.\d$/.test(val)) {
    alert('Invalid value');
  }
});

If you're not familiar with any javascript frameworks, and just want to get this working, try this:

<textareaonblur="validate(this.value);"></textarea><scripttype="text/javascript">functionvalidate(val) {
  if (/^\d{2}\.\d$/.test(val)) {
    alert('Invalid value');
  }
}
</script>

Purists, please don't punish me for adding this.

Post a Comment for "Validating A Text Box Value Using Javascript"