Only accpet (write to the input) valid value

These changes fix the bug #177.