Question: for example, I want the textfield input number only. my idea is when the user typing, it will call a javascript to check whether it is valid, if it is not valid, I delete the text which is just entered. But I think it is very complex to do so. Is there any simple way to do so?

Answer: 
Put a validation on submit.
I think, like o.k.w said, jQuery Masking would be a good choice because of 2 things:

1. jQuery is solid and fast
2. Masking is more user friendly than just deleting user input on the fly without any notification. Masking gives them a graphical reference of what type of input is expected and what the input might look like.